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

30 lines
721 B
Plaintext

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.