码迷,mamicode.com
首页 > 编程语言 > 详细

【python】类(资料+疑惑)

时间:2016-11-14 02:11:09      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:--   pass   运行时   资料   tor   span   nbsp   org   rip   

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 之间是怎么一种关系?

 

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

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)

 

【python】类(资料+疑惑)

标签:--   pass   运行时   资料   tor   span   nbsp   org   rip   

原文地址:http://www.cnblogs.com/dplearning/p/6060380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!