�� �� cppgx (setUID) �拇������到: ��
: Python 2.4.3 (#1, Jul 26 2006, 20:13:39)
: [GCC 3.4.6] on linux2
: Type "help", "copyright", "credits" or "license" for more information.
: >>> class t:
: ...     def __init__(self):
: ...         if not hasattr(t, 's'):
: ...             t.s = 0
: ...     def __str__(self):
: ...         t.s += 1
: ...         return '[%d]' % t.s
: ...     def __getattribute__(self, name):
: ...         if name == 's':
: ...             return t.s
: ...     def __setattr__(self, name, value):
: ...         if name == 's':
: ...             t.s = value
: ...
: >>> a = t()
: >>> b = t()
: >>> print a, a.s, b, b.s
: [1] 1 [2] 2
: >>> a.s = 5
: >>> print b, b.s, a, a.s
: [6] 6 [7] 7
: >>> 

If it's so useful, let's define it as a descriptor:

>>> class classattr(object):
..     def __init__(self, value):
..             self.value = value
..     def __get__(self, instance, owner):
..             return self.value
..     def __set__(self, instance, value):
..             self.value = value
..             
>>> class C(object):
..     n = classattr(0)
.. 
>>> c=C()
>>> c.n
0
>>> d=C()
>>> d.n=1
>>> c.n
1
>>> 

--
Numbers like 8 and 9 are useless for coding - everything
beyond 0 and 1 implies a flawed design.
--- Ville Vainio, in comp.lang.python


�� ��源:��水木���� newsmth.net��[FROM: 219.236.0.*]