corrections cours poo
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
Ojets en python
|
||||
Objets en python
|
||||
=================
|
||||
|
||||
un object est une entité possédant une identité, un état, et un comportement.
|
||||
|
29
algo/poo/cours/annexes/categorie.txt
Normal file
29
algo/poo/cours/annexes/categorie.txt
Normal file
@ -0,0 +1,29 @@
|
||||
types implicites et catégories
|
||||
==============================
|
||||
|
||||
Sometimes it is a good choice to use strings as implicit types.
|
||||
Some times not. Let us have some types :
|
||||
|
||||
::
|
||||
|
||||
types = ["booltype", "stringtype", "floattype"]
|
||||
if "booltype" in types:
|
||||
print type
|
||||
|
||||
But we loose an information there : all these string are the same type
|
||||
and not a string type.
|
||||
|
||||
::
|
||||
|
||||
class NormalizeType(str):
|
||||
pass
|
||||
typenames = ("booltype", "stringtype", "floattype")
|
||||
globs = globals()
|
||||
for typename in typenames:
|
||||
globs[typename] = NormalizeType(typename)
|
||||
# and for example
|
||||
if type(obj) == NormalizeType:
|
||||
print type
|
||||
|
||||
Now we don't rely on a list, the type names present here are just for the
|
||||
types's build. We rely on classes as sets.
|
@ -4,6 +4,8 @@ Annexes
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
types
|
||||
categorie
|
||||
ojbML
|
||||
ConstructionParFermeture
|
||||
cli
|
||||
|
33
algo/poo/cours/annexes/types.txt
Normal file
33
algo/poo/cours/annexes/types.txt
Normal file
@ -0,0 +1,33 @@
|
||||
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.
|
@ -37,8 +37,8 @@ def setup(app):
|
||||
app.add_config_value('correction', False, 'env')
|
||||
app.add_config_value('exercice', False, 'env')
|
||||
|
||||
exercice = False
|
||||
correction = False
|
||||
exercice = True
|
||||
correction = True
|
||||
|
||||
todo_include_todos = True
|
||||
# Add any paths that contain templates here, relative to this directory.
|
||||
|
Reference in New Issue
Block a user