Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no reasonable way to make this "class property" system to work in Python.</p> <p>Here is one unreasonable way to make it work. You can certainly make it more seamless with increasing amounts of metaclass magic.</p> <pre><code>class ClassProperty(object): def __init__(self, getter, setter): self.getter = getter self.setter = setter def __get__(self, cls, owner): return getattr(cls, self.getter)() def __set__(self, cls, value): getattr(cls, self.setter)(value) class MetaFoo(type): var = ClassProperty('getvar', 'setvar') class Foo(object): __metaclass__ = MetaFoo _var = 5 @classmethod def getvar(cls): print "Getting var =", cls._var return cls._var @classmethod def setvar(cls, value): print "Setting var =", value cls._var = value x = Foo.var print "Foo.var = ", x Foo.var = 42 x = Foo.var print "Foo.var = ", x </code></pre> <p>The knot of the issue is that properties are what Python calls "descriptors". There is no short and easy way to explain how this sort of metaprogramming works, so I must point you to the <a href="http://users.rcn.com/python/download/Descriptor.htm" rel="noreferrer">descriptor howto</a>.</p> <p>You only ever need to understand this sort of things if you are implementing a fairly advanced framework. Like a transparent object persistence or RPC system, or a kind of domain-specific language.</p> <p>However, in a comment to a previous answer, you say that you </p> <blockquote> <p>need to modify an attribute that in such a way that is seen by all instances of a class, and in the scope from which these class methods are called does not have references to all instances of the class.</p> </blockquote> <p>It seems to me, what you really want is an <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="noreferrer">Observer</a> design pattern.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload