1.http://python-china.org/t/77 有关method binding的理解
2.[Python] dir() 与 __dict__,__slots__ 的区别
7.详解Python中 __get__和__getattr__和__getattribute__的区别
8.定制类
9.Python 的 type 和 object 之间是怎么一种关系?
下面的几段代码为什么能够正确运行,运行时都发生了些什么???
class MyInt(int):
def __init__(self, v):
pass
def square(self):
return self * self def hello():
print 'hello' n = MyInt(2)
print n.__dict__
print MyInt.__dict__
n.hello = hello
n.hello()
print n.__dict__
import types
class Log(object):
def __init__(self, f):
self.f = f
def __get__(self,obj,cls):
print self.f.__name__, 'called'
return types.MethodType(self.f, obj, cls) class C(object):
@Log
def f(self):
pass
c = C()
c.f()
class C1(object):
a = 'abc'
def __getattribute__(self, *args, **kwargs):
print("__getattribute__() is called")
return object.__getattribute__(self, *args, **kwargs) def __getattr__(self, name):
print("__getattr__ is called")
return name + "from getattr" def __get__(self, instance, owner):
print("__get__() is called", instance, owner)
return self def foo(self, x):
print(x) class C2(object):
d = C1() if __name__ == "__main__":
c = C1()
c2 = C2()
print "====="
print(c.a)
print "------"
print(c.zzzz)
print "------"
c2.d
print "------"
print(c2.d.a)