Using property in classic class may not work

A. Lloyd Flanagan alloydflanagan at attbi.com
Wed Apr 16 17:41:28 EDT 2003


bokr at oz.net (Bengt Richter) wrote in message news:<b7hies$ol8$0 at 216.39.172.122>...
[lots of missing stuff]
> I may be fooling myself here, but a quick experiment suggests being
> able to define a new class that inherits from both a classic class
> that you can't touch and a new-style one that you can give a property, e.g.:
> 
>  Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
>  Type "help", "copyright", "credits" or "license" for more information.
>  >>> class Old3rdParty:
>  ...     def oldMethod(self): print 'oldMethod was called'
>  ...
>  >>> class NewPropertyCarrier(object):
>  ...     def __init__(self, v): self.prop = v
>  ...     def _get_p(self): return self._v
>  ...     def _set_p(self, v): self._v = v
>  ...     prop = property(_get_p, _set_p)
>  ...
>  >>> class Derived(Old3rdParty, NewPropertyCarrier):
>  ...     pass
>  ...
>  >>> d = Derived(123)
>  >>> d
>  <__main__.Derived object at 0x007D68E0>
>  >>> d.prop
>  123
>  >>> d.oldMethod()
>  oldMethod was called
>  >>> d.prop = 456
>  >>> d.prop
>  456
> 
> It seems like prop is surviving as a property, not getting clobbered into an attribute:
> 
>  >>> dir(d)
>  ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__',
>  '__module__', '__new__', '__reduce__', '__repr__', '__setattr__', '__str__', '__weakref__', '_ge
>  t_p', '_set_p', '_v', 'oldMethod', 'prop']
>  >>> vars(d)
>  {'_v': 456}
>  >>> d._v
>  456
>  >>> d.prop
>  456
>  >>> d._v = 'sneaky'
>  >>> d.prop
>  'sneaky'
>  >>> d.prop = 'normal'
>  >>> d.prop
>  'normal'
>  >>> d._v
>  'normal'
> 
> So could wxPython play the role of Old3rdParty above and allow building a Derived with
> the properties desired? Or is mixed bases a no-no?
> 
> Regards,
> Bengt Richter

That's a very clever idea.  I have no idea if it will work or not, but
definitely worth a try :)




More information about the Python-list mailing list