【python】類(資料+疑惑)

來源:互聯網
上載者:User

標籤:--   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 * selfdef hello():    print ‘hello‘n = MyInt(2)print n.__dict__print MyInt.__dict__n.hello = hellon.hello()print n.__dict__
import typesclass 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):        passc = 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】類(資料+疑惑)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.