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

I only know a little bit of Python so far, but I think it's things like the "underscore methods" (__len__, __cmp__, __init__) and the lack of blocks (which give Ruby a kind of elegant unity).

I do think, though, that Python's list comprehensions are quite beautiful, and I'm looking forward to using them.



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


Blocks are indisputably useful, but beautiful? There's no shortage of rants on the bizzare inconsistencies between blocks and proc and lambda (do I use call or yield? can I use return? why does arity checking rarely work?)


Maybe it's me but I find those underscore methods very helpful while skimming code.. They clearly point out the "magic functionality - probably adding syntactic sugar" declarations, helping me focus to the actual interface of the class.




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

Search: