Re: dynamic, metaclasses and Virtual properties
Sebastien Bigaret <[email protected]> 07 Aug 2004 15:30:46 +0200
| Newsgroups | gmane.comp.python.modeling |
|---|---|
| Message-ID | <[email protected]> |
Hi Erny and all,
On Wed, 30 Jun 2004, Ernesto Revilla <[email protected]> wrote:
[original post can be found at:
https://sf.net/mailarchive/forum.php?thread_id=5035017&forum_id=10674]
> I think that Python has a design error with properties, but it is easy to
> correct this.
[demonstration that properties aint behaving very good when it comes to
inheritance]
> In Modeling, when inheriting from class created with properties (dynamic), it
> does NOT the inherited methods. Is this reasonable?
You're right, this needs some attention, really. Could you submit this
as a bug report or as a patch proposal @sf.net please?
> Here I have attached a
> little patch for dynamic.py. I made a local function mkProp, so that variable
> binding is correct, otherwise it does not work the way it should.
Can you be more explicit on this point?
> Just a
> remark about the setter. As in the original code, a virtual setter is created
> only if the attribute is settable in the base class. In this case, we have two
> alternatives: 1. do not create a setter, as the original code, 2. create
> always a virtual setter and the setter in the base class raises a
> ReadOnlyAttributeException. (This is was I have done in a experimental
> framework.)
For the moment being we're not handling any 'readOnly' properties on
attributes --and the one in entity is still ignored for now, so I can't
really see whazt your point is, I suspect I'm misunderstanding something
here.
Oh, and BTW sorry for taking such a long time to answer :/
-- Sébastien.
> Index: dynamic.py
> ===================================================================
> RCS file: /cvsroot/modeling/ProjectModeling/Modeling/dynamic.py,v
> retrieving revision 1.1
> diff -u -r1.1 dynamic.py
> --- dynamic.py 16 Feb 2004 20:01:06 -0000 1.1
> +++ dynamic.py 30 Jun 2004 16:07:47 -0000
> @@ -144,10 +144,16 @@
> def add_properties(aClass, entity):
> for p in entity.classProperties():
> #print 'defining prop: ', p.name()
> - part_func_name=capitalizeFirstLetter(p.name())
> - prop=property(getattr(aClass, 'get'+part_func_name),
> - getattr(aClass, 'set'+part_func_name), None)
> - setattr(aClass, p.name(), prop)
> + part_func_name=capitalizeFirstLetter(p.name())
> + readonly=not hasattr(aClass,'set'+part_func_name)
> + def mkProp(name, readonly=0):
> + getter=lambda s: getattr(s,'get'+name)()
> + if readonly:
> + setter=None
> + else:
> + setter=lambda s, v: getattr(s,'set'+name)(v)
> + return property(getter, setter)
> + setattr(aClass, p.name(), mkProp(part_func_name, readonly))
>
> def build(model, define_properties=0):
> module_name=model.packageName()
-------------------------------------------------------
This SF.Net email is sponsored by OSTG. Have you noticed the changes on
Linux.com, ITManagersJournal and NewsForge in the past few weeks? Now,
one more big change to announce. We are now OSTG- Open Source Technology
Group. Come see the changes on the new OSTG site. www.ostg.com