class level properties

Arnaud Delobelle arnodel at googlemail.com
Sat Apr 12 16:18:57 EDT 2008


On Apr 12, 8:36 pm, Charles D Hixson <charleshi... at earthlink.net>
wrote:
> I'm trying to construct read-only variables at the class level.  I've
> been unsuccessful.  Any suggestions?
>
> Mixing @classmethod and @property doesn't appear to produce workable
> code.  Ditto for mixing @classmethod and __getattr__.  (The property
> approach compiles, but execution says that you can't execute properties.)
>
> I've got a rather large number of variables, so I don't want to define
> function accessors for each of them, and I *REALLY* don't want to have
> to access them as functions rather than variables or properties.

Metaclasses, of course!

>>> class MetaX(type):
...     @property
...     def spam(self): return 'eggs'
...
>>> class X(object):
...      __metaclass__ = MetaX
...
>>> X.spam
'eggs'
>>>

HTH

--
Arnaud




More information about the Python-list mailing list