May 27, 2016

[python] bound function got ya.

class MetaType(type):
    def __getattribute__(cls, field):
        print("Class name: {0}".format(cls))
        print("Field name: {0}".format(field))
        return type.__getattribute__(cls, field)


class Base(object):
    __metaclass__ = MetaType

    def function(cls):
        print("Class:{0}".format(cls))
    function = classmethod(function)


class Derived(Base):
    pass

if __name__ == "__main__":
    tmp_function = Base.function
    Derived.function()
    print(Derived.function)  # this function is Derived bound function
    Base.function = tmp_function
    Derived.function()
    print(Derived.function)  # this function is Base bound function

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.