Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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)



Is 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.


Sure, 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




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: