4 ms·
To some extent: the underscore methods represent 'magic behaviour' i.e. this gets called implicitly by something else, you never call this directly. i.e you wr
by tef__ 16y ago
To some extent: the underscore methods represent 'magic behaviour' i.e. this gets called implicitly by something else, you never call this directly.
i.e you write 4+2 to add things not 4.add(2)
- philwelch 16y agoIs this kind of magic irreversibly baked into Python, or can you make your own magic? One of the things I love about Ruby is that you can make your own magic, because a lot of the "magical" methods that you define (initialize and <=>, for instance) are just, by convention, the methods called by some module or class or whatnot, and you can write your own mixins or do metaprogramming to change up that behavior.
- orangecat 16y agoSure, you can do most of Ruby's wacky tricks in Python, it's just not usually recommended: >>> class Foo: ... def __init__(self, name): ... self.name=name >>> a1 = Foo('Alice') >>> a2 = Foo('Alice') >>> a1==a2 False >>> Foo.__eq__ = lambda self,other: self.name==other.name >>> a1==a2 True