How to do Obj-C Categories in Python?

后端 未结 3 1033
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 02:49

Obj-C (which I have not used for a long time) has something called categories to extend classes. Declaring a category with new methods and compiling it into your program, all in

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 03:50

    Why not just add methods dynamically ?

    >>> class Foo(object):
    >>>     pass
    >>> def newmethod(instance):
    >>>     print 'Called:', instance
    ...
    >>> Foo.newmethod = newmethod
    >>> f = Foo()
    >>> f.newmethod()
    Called: <__main__.Foo object at 0xb7c54e0c>
    

    I know Objective-C and this looks just like categories. The only drawback is that you can't do that to built-in or extension types.

提交回复
热议问题