[PIPython] Re: Dati statici

Carlo C8E Miron carlo.miron
Ven 25 Feb 2005 15:41:39 CET


> From: Luca Fabbri <lucafbb a gmail.com>
> To: python a lists.python.it
> Date: Fri, 25 Feb 2005 11:12:29 +0100
> Subject: [PIPython] Dati statici
> Salve a tutti.
> Provengo dal mondo di programmazione Java.
> Vorrei capire in Python come si definiscono dati (variabili, oggetti)
> statici all'interno di una classe.
> Per i metodi ho trovato staticmethod ma non riesco a trovare
> l'informazione per i dati comuni.

$ ipython 
Python 2.3.5 (#2, Feb  9 2005, 00:38:15) 
Type "copyright", "credits" or "license" for more information.

IPython 0.6.5 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: class spam(object):
   ...:     static_data = [1, 2, 3]
   ...:     def __init__(self, data):
   ...:         self.dynamic_data = data
   ...:         

In [2]: spam.static_data
Out[2]: [1, 2, 3]

In [3]: one = spam(42)

In [4]: two = spam(666)

In [5]: one.static_data, one.dynamic_data
Out[5]: ([1, 2, 3], 42)

In [6]: two.static_data, two.dynamic_data
Out[6]: ([1, 2, 3], 666)

In [7]: one.static_data.append(4)           

In [8]: two.static_data, two.dynamic_data
Out[8]: ([1, 2, 3, 4], 666)

HTH,
    C8E

PS: occhio alla differenza tra *mutare* ed effettuare il rebinding

-- 
Disclaimer:
If I receive a message from you, you are agreeing that:
1. I am by definition, "the intended recipient".
2. All information in the email is mine to do with as I see fit and
 make such financial profit, political mileage, or good joke as it
 lends itself to. In particular, I may quote it on USENET or the WWW.
3. I may take the contents as representing the views of your company.
4. This overrides any disclaimer or statement of confidentiality that
 may be included on your message.


More information about the Python mailing list