54.py 798 Bytes
Newer Older
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# o() and o.__call__() are not always equivalent due to the possibility of __getattribute__ or instance attributes:

def f():
    print "func"

class C(object):
    def __getattribute__(self, attr):
        print "__getattribute__", attr
        if attr == "__call__":
            return f
        return object.__getattribute__(self, attr)

    def __call__(self):
        print "__call__"

c = C()
c()
c.__call__()

print type
print type(1)

# As evidenced by the following real + important example:
# type() is a special function that gets the type of an object:
print type(C) # prints "<type 'type'>"
# but type.__call__() is the clsattr for any (non-metaclassed) class,
# which is how object creation gets handled:
print repr(type.__call__(C))[:20] # prints "<__main__.C object at 0xXXXXXXX>"