initialisation automatique de classe ? [résolu]

initialisation automatique de classe ? [résolu] - Python - Programmation

Marsh Posté le 01-11-2012 à 09:41:42    

Bonjour, voici un code qui fonctionne sans que je comprenne comment :

Code :
  1. class TupleOfTuples(tuple):
  2.     def __init__(self, src):
  3.         tuple.__init__(self)
  4. t = TupleOfTuples( ((1,2), (3,4), (5,6)) )
  5. print(t) # affiche bien : ((1,2), (3,4), (5,6))


Je ne comprends pas 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 .

Message cité 1 fois
Message édité par suizokukan le 11-12-2012 à 11:27:20

---------------
rule #1 : trust the python
Reply

Marsh Posté le 01-11-2012 à 09:41:42   

Reply

Marsh Posté le 01-11-2012 à 16:59:04    

suizokukan a écrit :

Bonjour, voici un code qui fonctionne sans que je comprenne comment :

Code :
  1. class TupleOfTuples(tuple):
  2.     def __init__(self, src):
  3.         tuple.__init__(self)
  4. t = TupleOfTuples( ((1,2), (3,4), (5,6)) )
  5. 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 :
  1. PyTypeObject PyTuple_Type = {
  2.    PyVarObject_HEAD_INIT(&PyType_Type, 0)
  3.    "tuple",
  4.    sizeof(PyTupleObject) - sizeof(PyObject *),
  5.    sizeof(PyObject *),
  6.    (destructor)tupledealloc,                   /* tp_dealloc */
  7.    (printfunc)tupleprint,                      /* tp_print */
  8.    0,                                          /* tp_getattr */
  9.    0,                                          /* tp_setattr */
  10.    0,                                          /* tp_compare */
  11.    (reprfunc)tuplerepr,                        /* tp_repr */
  12.    0,                                          /* tp_as_number */
  13.    &tuple_as_sequence,                         /* tp_as_sequence */
  14.    &tuple_as_mapping,                          /* tp_as_mapping */
  15.    (hashfunc)tuplehash,                        /* tp_hash */
  16.    0,                                          /* tp_call */
  17.    0,                                          /* tp_str */
  18.    PyObject_GenericGetAttr,                    /* tp_getattro */
  19.    0,                                          /* tp_setattro */
  20.    0,                                          /* tp_as_buffer */
  21.    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  22.        Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
  23.    tuple_doc,                                  /* tp_doc */
  24.    (traverseproc)tupletraverse,                /* tp_traverse */
  25.    0,                                          /* tp_clear */
  26.    tuplerichcompare,                           /* tp_richcompare */
  27.    0,                                          /* tp_weaklistoffset */
  28.    tuple_iter,                                 /* tp_iter */
  29.    0,                                          /* tp_iternext */
  30.    tuple_methods,                              /* tp_methods */
  31.    0,                                          /* tp_members */
  32.    0,                                          /* tp_getset */
  33.    0,                                          /* tp_base */
  34.    0,                                          /* tp_dict */
  35.    0,                                          /* tp_descr_get */
  36.    0,                                          /* tp_descr_set */
  37.    0,                                          /* tp_dictoffset */
  38.    0,                                          /* tp_init */
  39.    0,                                          /* tp_alloc */
  40.    tuple_new,                                  /* tp_new */
  41.    PyObject_GC_Del,                            /* tp_free */
  42. };


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, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 01-11-2012 à 23:04:43    

Merci Masklinn pour cette explication très complète : j'ignorais complètement cela !
 
Merci aussi à ceux qui m'on lu !


---------------
rule #1 : trust the python
Reply

Marsh Posté le 02-11-2012 à 08:43:38    

When in doubt, check the source :D
 
(en l'occurence dans ce cas précis c'est documenté... sauf que c'est documenté dans __new__ donc il faut soit avoir lu toute la doc du data model soit avoir un gros coup de bol)


---------------
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, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 02-11-2012 à 14:35:07    

En effet, merci encore de ces précisions !


---------------
rule #1 : trust the python
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed