property for class objects

Steve Menard foo at bar.com
Tue Nov 9 23:08:44 EST 2004


Jp Calderone wrote:
> 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

I had thought about this solution. However, that would mean I would have 
to define a Python metaclass (to hold the class properties) for each 
Java class uses, as well as the Python class I already generate. I am 
afraid this would en up being very heavy.

I am not discarding this solution completely. It migth be that defining 
those 2 (class and metaclass) isnt too heavy. Someone needs to clue me 
in on this or I have to do some benchmarking myself.

Hopefully, tehre is a cleaner way to solve this.

Steve



More information about the Python-list mailing list