Hacker News new | past | comments | ask | show | jobs | submit login

I know very little of Ruby, but difference 13 states that Ruby has stronger metaprogramming features in that "Python doesn't have define_method, class_eval, and method_missing". Are these really things that can't be implemented with metaclasses and overloading __getattr__?



* method_missing is "return a callable from __getattr__"

* define_method simply isn't needed, just set a function as attribute on a class tadaa you've defined a method:

    >>> class A: pass
    ... 
    >>> a = A()
    >>> a.foo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'A' object has no attribute 'foo'
    >>> A.foo = lambda self: 3
    >>> a.foo
    <bound method A.<lambda> of <__main__.A object at 0x100623c10>>
    >>> a.foo()
    3
* class_eval I never really understood the use case for, if it's just to add new methods to an existing class, take the previous recipe and annotate the function with the `classmethod` decorator:

    >>> A.bar = classmethod(lambda cls: 4)
    >>> A.bar()
    4
    >>> a.bar()
    4
but there might be more to it I missed.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: