property for class objects

Jp Calderone exarkun at divmod.com
Tue Nov 9 22:22:22 EST 2004


On Tue, 09 Nov 2004 21:54:50 -0500, Steve Menard <foo at bar.com> wrote:
>As par of JPype, I find that I have to brifge Java static members into 
> class attributes.
> 
> I am using a metaclass to dynamically create a Python class for every 
> Java type encountered. Instance variables were handled by adding a 
> property(get, set) to the class's dictionary.
> 
> Now static members are giving me a headache. I cannot use "property" for 
> them, because properties are for instances only.
> 
> What I am currently doing is adding a __getattr__ and a __setattr__ 
> methods to the metaclass, which is called when a class attribute is 
> requested but not found in the class' dict.
> 
> This works fine exact it does not walk the inheritance tree to find 
> static members defined in base classes. I am currently adding up all the 
> names in every class. This "works", however it is misleading when you do 
> a dir() of the class.
> 
> Does anyone have an idea how to implement "classproperty" ?

  How about this?

    exarkun at boson:~$ python
    Python 2.3.4 (#2, Sep 24 2004, 08:39:09) 
    [GCC 3.3.4 (Debian 1:3.3.4-12)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class x(type):
    ...     def get_y(self):
    ...             return 'y on', self
    ...     def set_y(self, value):
    ...             print 'y on', self, 'is now', value
    ...     y = property(get_y, set_y)
    ... 
    >>> class z(object):
    ...         __metaclass__ = x
    ... 
    >>> z.y
    ('y on', <class '__main__.z'>)
    >>> z.y = 'foo'
    y on <class '__main__.z'> is now foo
    >>> 

  If not, how is what you are looking for different?

  Jp



More information about the Python-list mailing list