Guest User

Untitled

a guest
Jul 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. >>> class Test(object):
  2. ... a = ''
  3. ... def __new__(clazz, a): #インスタンス生成
  4. ... print 'new'
  5. ... #__new__はインスタンスではなくクラスを引数に取る(インスタンス生成前だから)
  6. ... #この__new__が呼び出されたコンテキスト内(clazz)で、Testクラスのスーパークラス呼び出し
  7. ... return super(Test, clazz).__new__(clazz) #object.__new__(clazz)と同義
  8. ... def __init__(self, a): #インスタンス生成後のコンストラクタ
  9. ... self.a = a
  10. ... print 'init'
  11. ... def __call__(self): #インスタンスに対する'呼び出し'
  12. ... print 'call'
  13. ... def p(self): #インスタンスメソッド
  14. ... print self.a
  15. ...
  16. >>> a = Test(1)
  17. new
  18. init
  19. >>> a.a
  20. 1
  21. >>> a()
  22. call
  23. >>> a.p
  24. <bound method Test.p of <__main__.Test object at 0x2b5256272f90>>
  25. >>> a.p()
  26. 1
Add Comment
Please, Sign In to add comment