formations/algo/poo/cours/annexes/types.txt

34 lines
692 B
Plaintext

type et d'objet en python
=========================
et voir la confusion qui est faite avec le mélange classe et objets en python
In python, base types cannot be inherited, for example:
<pre>
>>> class MyType(int):
... pass
...
</pre>
But, they are not types any more, but objects. They can have attributes (types
cannot have attributes). They cannot return anything at the init (a type has to
return something at the init, it is the instance itself).
<pre>
>>> t = MyType(2)
>>> t
2
>>> isinstance(t, MyType)
True
>>> isinstance(t, int)
True
>>> isinstance(MyType, type)
True
>>>
>>> isinstance(t, object)
True
</pre>
So MyType is a type but has the notation behavior of an object.