masklinn í dag viðrar vel til loftárása | suizokukan a écrit :
Bonjour, voici un code qui fonctionne sans que je comprenne comment :
Code :
- class TupleOfTuples(tuple):
- def __init__(self, src):
- tuple.__init__(self)
- t = TupleOfTuples( ((1,2), (3,4), (5,6)) )
- print(t) # affiche bien : ((1,2), (3,4), (5,6))
|
Je ne comprends comment se passe l'initialisation de ma classe TupleOfTuples; la méthode __init__ n'utilise pas src et je ne vois nulle part comment l'argument ((1,2), (3,4), (5,6)) est passé à la classe-mère tuple. Quelqu'un pourrait-il m'éclairer ? Merci d'avance ! PS : même comportement avec Python 2.x et 3.x .
|
Code :
PyTypeObject PyTuple_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "tuple", sizeof(PyTupleObject) - sizeof(PyObject *), sizeof(PyObject *), (destructor)tupledealloc, /* tp_dealloc */ (printfunc)tupleprint, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ (reprfunc)tuplerepr, /* tp_repr */ 0, /* tp_as_number */ &tuple_as_sequence, /* tp_as_sequence */ &tuple_as_mapping, /* tp_as_mapping */ (hashfunc)tuplehash, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */ tuple_doc, /* tp_doc */ (traverseproc)tupletraverse, /* tp_traverse */ 0, /* tp_clear */ tuplerichcompare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ tuple_iter, /* tp_iter */ 0, /* tp_iternext */ tuple_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ tuple_new, /* tp_new */ PyObject_GC_Del, /* tp_free */ };
|
Un tuple est immutable, donc toute l'initialization du type c tuple est faite dans __new__ (le slot tp_new), pas dans init: le temps qu'__init__ s'exécute il n'est déjà plus possible de toucher le truc.
Message édité par masklinn le 01-11-2012 à 16:59:20 ---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
|