ajout de doc sur les classes et les objets
This commit is contained in:
parent
5b42326506
commit
ad27c7704e
|
@ -72,6 +72,124 @@ possibilité en python d'héritage multiple::
|
|||
class A(B, C): pass
|
||||
|
||||
|
||||
attribut d'objets et de classes
|
||||
----------------------------------
|
||||
|
||||
>>> o = object()
|
||||
>>> o
|
||||
<object object at 0x7f77c9cda0d0>
|
||||
>>> class C(object): pass
|
||||
...
|
||||
>>> class C: pass
|
||||
...
|
||||
>>> c = C()
|
||||
>>> c.a = 3
|
||||
>>> c.a
|
||||
3
|
||||
>>> vars(c)
|
||||
{'a': 3}
|
||||
>>> c.__dict__
|
||||
{'a': 3}
|
||||
>>> C.__dict__
|
||||
{'__module__': '__main__', '__doc__': None}
|
||||
>>> C.c = 5
|
||||
>>> C.__dict__
|
||||
{'c': 5, '__module__': '__main__', '__doc__': None}
|
||||
>>> c.c
|
||||
5
|
||||
>>> c.z = 3
|
||||
>>> c.z
|
||||
3
|
||||
>>> c.__dict__
|
||||
{'a': 3, 'z': 3}
|
||||
>>> C.__dict__
|
||||
{'c': 5, '__module__': '__main__', '__doc__': None}
|
||||
>>> class MaKlass:
|
||||
... def unefonction(self, x):
|
||||
... print x
|
||||
...
|
||||
>>> MaKlass.__dict__
|
||||
{'__module__': '__main__', '__doc__': None, 'unefonction': <function unefonction at 0x7f77c5b0c488>}
|
||||
>>> k = MaKlass()
|
||||
>>> k.__dict__
|
||||
{}
|
||||
>>> def autrefonc(self, x)
|
||||
File "<stdin>", line 1
|
||||
def autrefonc(self, x)
|
||||
^
|
||||
SyntaxError: invalid syntax
|
||||
>>> def autrefonc(self, x):
|
||||
... print x
|
||||
...
|
||||
>>> k.autrefonc = autrefonc
|
||||
>>> k.__dict__
|
||||
{'autrefonc': <function autrefonc at 0x7f77c5b0c500>}
|
||||
>>> MaKlass.__dict__
|
||||
{'__module__': '__main__', '__doc__': None, 'unefonction': <function unefonction at 0x7f77c5b0c488>}
|
||||
>>> MaKlass.unefonction(k, "toto")
|
||||
toto
|
||||
>>> k.unefonction("toto")
|
||||
toto
|
||||
>>> k.__dict__
|
||||
{'autrefonc': <function autrefonc at 0x7f77c5b0c500>}
|
||||
>>> MaKlass.toto = "test"
|
||||
>>> k.__dict__
|
||||
{'autrefonc': <function autrefonc at 0x7f77c5b0c500>}
|
||||
>>> k.toto
|
||||
'test'
|
||||
>>>
|
||||
|
||||
|
||||
le __dict__ avec l'héritage de classe
|
||||
-------------------------------------------
|
||||
|
||||
|
||||
>>> class A(object): pass
|
||||
...
|
||||
>>> A.__dict__
|
||||
dict_proxy({'__dict__': <attribute '__dict__' of 'A' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None})
|
||||
>>> class B(A):
|
||||
... b = 3
|
||||
...
|
||||
>>> class C(B):
|
||||
... c = 2
|
||||
...
|
||||
>>> c = C()
|
||||
>>> o = C()
|
||||
>>> o.__dict__
|
||||
{}
|
||||
>>> o.c
|
||||
2
|
||||
>>> o.b
|
||||
3
|
||||
>>> o.__class__
|
||||
<class '__main__.C'>
|
||||
>>> o.__class__.__dict__
|
||||
dict_proxy({'__module__': '__main__', 'c': 2, '__doc__': None})
|
||||
>>>
|
||||
|
||||
|
||||
method resolution object
|
||||
-----------------------------
|
||||
|
||||
>>> class A(object): pass
|
||||
...
|
||||
>>> A.__mro__
|
||||
(<class '__main__.A'>, <type 'object'>)
|
||||
>>> class B(A): pass
|
||||
...
|
||||
>>> B.__mro__
|
||||
(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)
|
||||
>>> class C(A, B): pass
|
||||
...
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
TypeError: Error when calling the metaclass bases
|
||||
Cannot create a consistent method resolution
|
||||
order (MRO) for bases A, B
|
||||
>>>
|
||||
|
||||
|
||||
introspection contre encapsulation
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
|
Loading…
Reference in New Issue