>>> class C ( object ):
... pass
...
>>> c = C()
>>> c . __call__ = lambda : 42
>>> c()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
TypeError : 'C' object is not callable
>>> C . __call__ = lambda self : 42
>>> c()
42
>>> class A ( object ): pass
...
>>> a = A()
>>> a . x = 1
>>> a
<__main__.A object at 0x7fafa9b26f90>
>>> setattr (a, 'y' , 2 )
>>> a . __dict__
{'y': 2, 'x': 1}
>>> a . __setattr__( 'z' , 3 )
>>> a . __dict__
{'y': 2, 'x': 1, 'z': 3}
>>> a . __setattr__ = lambda self : 42
>>> a . __setattr__( 'z' , 4 )
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
TypeError : <lambda>() takes exactly 1 argument (2 given)
>>> setattr (a, 'foo' , 'bar' )
>>> a . __dict__
{'y': 2, 'x': 1, '__setattr__': <function <lambda> at 0x7fafa9b3a140>, 'z': 3, 'foo': 'bar'}
>>> A . __setattr__ = lambda self : 42
>>> setattr (a, 'baz' , 'quux' )
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
TypeError : <lambda>() takes exactly 1 argument (3 given)
>>> class A ( object ):
... def __setattr__ ( self , attr, value):
... print 'for instances' , attr, value
... object . __setattr__( self , attr, value)
...
>>> a = A()
>>> a . __setattr__( 'x' , 1 )
for instances x 1
>>> a . __dict__
{'x': 1}
>>> A . __setattr__( 'foo' , 'bar' )
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
TypeError : unbound method __setattr__() must be called with A instance as first argument (got str instance instead)
>>> A . __dict__[ '__setattr__' ]
<function __setattr__ at 0x7f699d22fa28>
>>> class C ( object ):
... pass
...
>>> ()
<__main__.C object at 0x1121e10>
>>> type (C) . __call__(C)
<__main__.C object at 0x1121ed0>
>>> class SingletonMeta ( type ):
... def __call__ (cls, * args, ** kw):
... return super (SingletonMeta, cls) . __call__( * args, ** kw)
...
>>>
>>> class SingletonMeta ( type ):
... def __init__ (cls, * args, ** kw):
... cls . instance = None
... def __call__ (cls, * args, ** kw):
... return super (SingletonMeta, cls) . __call__( * args, ** kw)
...
>>>
__call__:
>>> class SingletonMeta ( type ):
... def __init__ (cls, * args, ** kw):
... cls . instance = None
... def __call__ (cls, * args, ** kw):
... if cls . instance is None :
... cls . instance = super (SingletonMeta, cls) . __call__( * args, ** kw)
... return cls . instance
...
>>> class C ( object ):
... __metaclass__ = SingletonMeta
...
>>> C() is C()
True
>>> a = C()
>>> b = C()
>>> a . x = 42
>>> b . x
42
>>>
>>> def mymeta (name, bases, attrs):
... attrs[ 'foo' ] = 'bar'
... return type (name, bases, attrs)
...
>>> class D ( object ):
... __metaclass__ = mymeta
...
>>> D()
<__main__.D object at 0x7fafa9abc090>
>>> d = D()
>>> d . foo
'bar'
>>> d . __dict__
{}
>>> D . __dict__
<dictproxy object at 0x7fafa9b297f8>
>>> dict (D . __dict__)
{'__module__': '__main__', '__metaclass__': <function mymeta at 0x7fafa9b3a9b0>, '__dict__': <attribute '__dict__' of 'D' objects>, 'foo': 'bar', '__weakref__': <attribute '__weakref__' of 'D' objects>, '__doc__': None}
>>> if True :
... class A ( object ):
... def foo ( self ):
... print 42
...
>>> A
<class '__main__.A'>
>>> A() . foo()
42
>>>
>>> class A ( object ):
... if 1 > 2 :
... def foo ( self ):
... print '1>2'
... else :
... def bar ( self ):
... print 'else'
...
>>>
>>> A()
<__main__.A object at 0x7fafa9abc150>
>>> A() . foo()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
AttributeError : 'A' object has no attribute 'foo'
>>> A() . bar()
else
>>> class A ( object ):
... if 1 > 2 :
... x = 1
... def foo ( self ):
... print 'if'
... else :
... y = 1
... def bar ( self ):
... print 'else'
...
>>> A . x
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
AttributeError : type object 'A' has no attribute 'x'
>>> A . y
1
>>> A . foo
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
AttributeError : type object 'A' has no attribute 'foo'
>>> A . bar
<unbound method A.bar>
>>> A . bar()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
TypeError : unbound method bar() must be called with A instance as first argument (got nothing instead)
>>> A() . bar()
else
>>>
>>> class A ( object ):
... class B ( object ):
... pass
...
...
>>> A()
<__main__.A object at 0x7fafa9abc2d0>
>>> A . __dict__
<dictproxy object at 0x7fafa9b340f8>
>>> dict (A . __dict__)
{'__dict__': <attribute '__dict__' of 'A' objects>, '__module__': '__main__', 'B': <class '__main__.B'>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
>>> A . B()
<__main__.B object at 0x7fafa9abc310>
>>> FIELDS = [ 'a' , 'b' , 'c' ]
>>> class A ( object ):
... for f in FIELDS:
... locals ()[f] = lambda self : 42
...
>>> a = A()
>>> a . a()
42
>>> a . b()
42
>>> a . c()
42
>>> a . d()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
AttributeError : 'A' object has no attribute 'd'
>>>
>>> class A ( object ):
... __private_foo =1
...
>>> A . __private_foo
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
AttributeError : type object 'A' has no attribute '__private_foo'
>>> A . _A__private_foo
1
>>> dict (A . __dict__)
{'__dict__': <attribute '__dict__' of 'A' objects>, '_A__private_foo': 1, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
>>>
>>> class B (A):
... def foo ( self ):
... print self . __private_foo
...
>>> B() . foo()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
File "<stdin>" , line 3 , in foo
AttributeError : 'B' object has no attribute '_B__private_foo'
>>> class C ( object ):
... def __init__ ( self ):
... self . __dict__[ '__value' ] = 1
...
>>> C() . __value
1
>>>
>>> class D ( object ):
... def __init__ ( self ):
... self . c = C() . __value
...
>>> D()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
File "<stdin>" , line 3 , in __init__
AttributeError : 'C' object has no attribute '_D__value'
>>> C() . __value
1
>>>
Source: https://habr.com/ru/post/114587/
All Articles