【python】类(资料+疑惑)

1.http://python-china.org/t/77 有关method binding的理解

2.[Python] dir() 与 __dict__,__slots__ 的区别

3.Descriptor HowTo Guide

4.如何理解 Python 的 Descriptor?

5.简明Python魔法 - 1

6.简明Python魔法 - 2

7.详解Python中 __get__和__getattr__和__getattribute__的区别

8.定制类

9.Python 的 type 和 object 之间是怎么一种关系?

10.python MethodType方法详解和使用

下面的几段代码为什么能够正确运行,运行时都发生了些什么???

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)
上一篇:jQuery基础(1) -- jQuery 语法


下一篇:Java--静态区域块