Linuxtrent: Re: Qualche domanda su Python...

  • From: Lele Gaifax <lele@xxxxxxxxxx>
  • To: linuxtrent@xxxxxxxxxxxxx
  • Date: Mon, 21 May 2001 16:21:35 +0200

>>>>> On Mon, 21 May 2001 15:41:17 +0200, Matteo Ianeselli <ianezz@xxxxxxxxxx> 
>>>>> said:

    MI> Uhm, chissa` se fra i proposal c'e` qualcosa: dopotutto, se
    MI> vale la distinzione tra ``Nomeclasse.attributo'' e
    MI> ``istanza.attributo'' (vedi sopra), perche` non dovrebbe
    MI> valere la stessa cosa anche per i metodi?

Devi prestare attenzione ai `namespace', che sono la modalità di
Python di fare il binding tra un nome di una variabile e un oggetto:
nel caso di una istanza di classe, si ha

`Class instances'

     A class instance is created by calling a class object (see
     above).  A class instance has a namespace implemented as a
     dictionary which is the first place in which attribute references
     are searched.  When an attribute is not found there, and the
     instance's class has an attribute by that name, the search
     continues with the class attributes.  If a class attribute is
     found that is a user-defined function object (and in no other
     case), it is transformed into an unbound user-defined method
     object (see above).  The `im_class' attribute of this method
     object is the class in which the function object was found, not
     necessarily the class of the instance for which the attribute
     reference was initiated.  If no class attribute is found, and the
     object's class has a `__getattr__()' method, that is called to
     satisfy the lookup.

     Attribute assignments and deletions update the instance's
     dictionary, never a class's dictionary.  If the class has a
     `__setattr__()' or `__delattr__()' method, this is called instead
     of updating the instance dictionary directly.

Non è quindi tecnicamente giusto parlare di "distizione" tra
classe/istanza. E' corretto dire che quando un'istanza di una classe
fa il lookup di una proprietà, cerca prima nel proprio namespace, poi
in quello della/e classe/i di appartenza. Quando però definisci una
proprietà, questa va a finire esclusivamente nel namespace
dell'istanza in questione. Le altre istanze della classe continueranno
a vedere la proprietà originaria della classe.

Come detto, se ne parla parecchio in lista, almeno fino ad una
settimana fa c'era un lunghissimo thread sull'argomento: si tratta di
una funzionalità particolarmente richiesta dal gruppo di Zope...

ciao, lele.
-- 
nickname: Lele Gaifax   | Quando vivro' di quello che ho pensato ieri
real: Emanuele Gaifas   | comincero' ad aver paura di chi mi copia.
email: lele@xxxxxxxxxx  |               -- Fortunato Depero, 1929.

-- 
Per iscriversi  (o disiscriversi), basta spedire un  messaggio con SOGGETTO
"subscribe" (o "unsubscribe") a mailto:linuxtrent-request@xxxxxxxxxxxxxxxxx


Other related posts: