Advertisement
Guest User

Untitled

a guest
May 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. In [10]: class A(object): pass
  2.    ....:
  3.  
  4. In [11]: def f(self, x): print 'f called:', self, x
  5.    ....:
  6.  
  7. In [12]: A.f = f
  8.  
  9. In [13]: a = A()
  10.  
  11. In [14]: a.f(2)
  12. f called: <__main__.A object at 0x1b6a910> 2
  13.  
  14. In [15]: a.f.__f
  15. a.f.__format__  a.f.__func__    
  16.  
  17. In [15]: a.f.__func__.__name__
  18. Out[15]: 'f'
  19.  
  20. In [16]: a.__class__
  21. Out[16]: <class '__main__.A'>
  22.  
  23. In [17]: a.__class__.__name__
  24. Out[17]: 'A'
  25.  
  26. In [18]: dir(a)
  27. Out[18]:
  28. ['__class__',
  29.  '__delattr__',
  30.  '__dict__',
  31.  '__doc__',
  32.  '__format__',
  33.  '__getattribute__',
  34.  '__hash__',
  35.  '__init__',
  36.  '__module__',
  37.  '__new__',
  38.  '__reduce__',
  39.  '__reduce_ex__',
  40.  '__repr__',
  41.  '__setattr__',
  42.  '__sizeof__',
  43.  '__str__',
  44.  '__subclasshook__',
  45.  '__weakref__',
  46.  'f']
  47.  
  48. In [19]: del A.f
  49.  
  50. In [20]: a.f()
  51. ---------------------------------------------------------------------------
  52. AttributeError                            Traceback (most recent call last)
  53.  
  54. /home/me/<ipython console> in <module>()
  55.  
  56. AttributeError: 'A' object has no attribute 'f'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement