473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

change property after inheritance

Suppose a class has properties and I want to change the
setter in a derived class. If the base class is mine, I can do this:
http://www.kylev.com/2004/10/13/fun-...on-properties/
Should I? (I.e., is that a good solution?)

And what if I cannot change the base class?
How to proceed then?

Thanks,
Alan Isaac
Sep 6 '06 #1
7 1716
Le mercredi 06 septembre 2006 16:33, David Isaac a écrit*:
Suppose a class has properties and I want to change the
setter in a derived class. If the base class is mine, I can do this:
http://www.kylev.com/2004/10/13/fun-...on-properties/
Should I? (I.e., is that a good solution?)
Why not ? This ontroduce the notion of public getter a la C++/Java while the
property is overloadable by itself (as below), but it's correct design IMHO.
And what if I cannot change the base class?
How to proceed then?
Like that :

In [44]: class a(object) :
....: p=property(lambda s : getattr(s, '_p', None))
....:
....:

In [46]: class b(a) :
....: p=property(a.p.fget, lambda s, v : setattr(s, '_p', v))
....:
....:

In [47]: print a().p
None

In [48]: a().p = 5
---------------------------------------------------------------------------
exceptions.AttributeError Traceback (most recent
call last)

/home/maric/<ipython console>

AttributeError: can't set attribute

In [49]: ib=b()

In [50]: print ib.p
None

In [51]: ib.p = 5

In [52]: print ib.p
5

In [53]: ib._p
Out[53]: 5
--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
Sep 6 '06 #2

Le mercredi 06 septembre 2006 16:33, Alan Isaac a écrit :
>Suppose a class has properties and I want to change the
setter in a derived class. If the base class is mine, I can do this:
http://www.kylev.com/2004/10/13/fun-...on-properties/
Should I? (I.e., is that a good solution?)
"Maric Michaud" <ma***@aristote.infowrote in message
news:ma************************************@python .org...
Why not ? This ontroduce the notion of public getter a la C++/Java while
the
property is overloadable by itself (as below), but it's correct design
IMHO.

More support for lambda, it seems...
Cheers,
Alan Isaac
Sep 7 '06 #3
David Isaac wrote:
Le mercredi 06 septembre 2006 16:33, Alan Isaac a écrit :
>>Suppose a class has properties and I want to change the
setter in a derived class. If the base class is mine, I can do this:
http://www.kylev.com/2004/10/13/fun-...on-properties/
Should I? (I.e., is that a good solution?)

"Maric Michaud" <ma***@aristote.infowrote in message
news:ma************************************@python .org...
>Why not ? This ontroduce the notion of public getter a la C++/Java while
the
>property is overloadable by itself (as below), but it's correct design
IMHO.

More support for lambda, it seems...
Well, lambda's not going away[1], but there's no *need* for lambda here.
It could be written as::

class Base(object):
def __init__(self):
self.foo = None

def getFoo(self):
return self.__foo

def setFoo(self, val):
self.__foo = val

def _prop_get_foo(self):
return self.getFoo()

def _prop_set_foo(self, val):
return self.setFoo(val)

foo = property(fget=_prop_get_foo, fset=_prop_set_foo)

[1] http://www.python.org/dev/peps/pep-3099/

STeVe
Sep 7 '06 #4
Steven Bethard wrote:
David Isaac wrote:
Le mercredi 06 septembre 2006 16:33, Alan Isaac a écrit :
>Suppose a class has properties and I want to change the
setter in a derived class. If the base class is mine, I can do this:
http://www.kylev.com/2004/10/13/fun-...on-properties/
Should I? (I.e., is that a good solution?)
"Maric Michaud" <ma***@aristote.infowrote in message
news:ma************************************@python .org...
Why not ? This ontroduce the notion of public getter a la C++/Java while
the
property is overloadable by itself (as below), but it's correct design
IMHO.

More support for lambda, it seems...

Well, lambda's not going away[1], but there's no *need* for lambda here.
It could be written as::
Sure, it *could*; whether it *should* is a different issue. I can't
imagine a case for absolute *need* of lambda, but there are several
cases where it is probably the best way, such as the one of this
thread.

George

Sep 7 '06 #5
George Sakkis wrote:
Steven Bethard wrote:
>David Isaac wrote:
>>Le mercredi 06 septembre 2006 16:33, Alan Isaac a écrit :
Suppose a class has properties and I want to change the
setter in a derived class. If the base class is mine, I can do this:
http://www.kylev.com/2004/10/13/fun-...on-properties/
Should I? (I.e., is that a good solution?)
"Maric Michaud" <ma***@aristote.infowrote in message
news:ma************************************@pyth on.org...
Why not ? This ontroduce the notion of public getter a la C++/Java while
the
property is overloadable by itself (as below), but it's correct design
IMHO.

More support for lambda, it seems...
Well, lambda's not going away[1], but there's no *need* for lambda here.
It could be written as::

Sure, it *could*; whether it *should* is a different issue. I can't
imagine a case for absolute *need* of lambda, but there are several
cases where it is probably the best way, such as the one of this
thread.
I'd contend that the "right" solution in this particular case is to use
a descriptor that creates a property with late-binding lookup. That way
you don't even need the extra function call that the lambda is
providing. Try using one of the following recipies:

http://aspn.activestate.com/ASPN/Coo.../Recipe/408713
http://aspn.activestate.com/ASPN/Coo.../Recipe/442418

That's not to say that there aren't other situations where a lambda
might be the best solution.

STeVe
Sep 7 '06 #6
Le jeudi 07 septembre 2006 15:33, Steven Bethard a écrit*:
Well, lambda's not going away[1],
Sure, they won't.
but there's no *need* for lambda here.
* It could be written as::
Le jeudi 07 septembre 2006 17:16, George Sakkis a écrit :
Sure, it *could*; whether it *should* is a different issue. I can't
imagine a case for absolute *need* of lambda, but there are several
cases where it is probably the best way, such as the one of this
thread.
I have no preferences here, I used lambdas because it's more compact but they
have also their drawback, when the function get a little more complex the
code is quickly confusing. The main advantage of the lambdas in this case is
to not pollute the class namespace.

Le jeudi 07 septembre 2006 23:48, Steven Bethard a écrit :
Try using one of the following recipies:

http://aspn.activestate.com/ASPN/Coo.../Recipe/408713
http://aspn.activestate.com/ASPN/Coo.../Recipe/442418
The code i wrote was to demonstrate late binding is usually not needed (and
it's not the semantic of properties so it's a bit like "make Java in
Python").
Only the second recipe has to do with it, but is not clean IMHO, it's
unnecessary complicated and it introduce a extra level of indentation which
is rather confusing, the 'self' variable in accessors is not what it seems to
be. Moreover, it introduce a new semantic for a functionality which is
already part of the language, what's the goal ? To lost python developers
reading your code ?

If you really want late binding, the first recipe may be a solution, but it
should be both simpler and should not introduce a new semantic (the functions
passed as strings is disappointing).
I'd write it like this :

class LateBindingProperty(property) :
__doc__ = property.__dict__['__doc__'] # see bug #576990

def __init__(self, fget=None, fset=None, fdel=None, doc=None) :
if fget : fget = lambda s, n=fget.__name__ : getattr(s, n)()
if fset : fset = lambda s, v, n=fset.__name__ : getattr(s, n)(v)
if fdel : fdel = lambda s, n=fdel.__name__ : getattr(s, n)()
property.__init__(self, fget, fset, fdel, doc)


In [4]: class A(object) :
...: def getx(self) : return self._x
...: def setx(self, v) : self._x = v
...: p=LateBindingProperty(getx, setx)
...:
...:

In [5]: class B(A) :
...: def setx(self, v) : A.setx(self, 2*v)
...:
...:

In [8]: a=A()

In [9]: a.p = 5

In [10]: a.p
Out[10]: 5

In [11]: a._x
Out[11]: 5

In [12]: b=B()

In [13]: b.p=5

In [14]: b.p
Out[14]: 10

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
Sep 8 '06 #7
Maric Michaud wrote:
Le jeudi 07 septembre 2006 15:33, Steven Bethard a écrit :
>Well, lambda's not going away[1],

Sure, they won't.
>but there's no *need* for lambda here.
It could be written as::

Le jeudi 07 septembre 2006 17:16, George Sakkis a écrit :
>Sure, it *could*; whether it *should* is a different issue. I can't
imagine a case for absolute *need* of lambda, but there are several
cases where it is probably the best way, such as the one of this
thread.

I have no preferences here, I used lambdas because it's more compact but they
have also their drawback, when the function get a little more complex the
code is quickly confusing. The main advantage of the lambdas in this case is
to not pollute the class namespace.

Le jeudi 07 septembre 2006 23:48, Steven Bethard a écrit :
> Try using one of the following recipies:

http://aspn.activestate.com/ASPN/Coo.../Recipe/408713
http://aspn.activestate.com/ASPN/Coo.../Recipe/442418

The code i wrote was to demonstrate late binding is usually not needed (and
it's not the semantic of properties so it's a bit like "make Java in
Python").
If you're really this uncomfortable with writing your own descriptors,
sure, you don't have to. But descriptors are an integral part of Python
-- new-style classes wouldn't have methods without them, nor could
classmethod or staticmethod have been defined. So defining a new
descriptor is far from un-Pythonic.
If you really want late binding, the first recipe may be a solution, but it
should be both simpler and should not introduce a new semantic (the functions
passed as strings is disappointing).
If you want to use the functions instead of their names, it's as simple
as changing the __init__ to:

def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.getname = fget.__name__
self.setname = fset.__name__
self.delname = fdel.__name__
self.__doc__ = doc

Then you can use the same signature as property.

STeVe
Sep 8 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Laszlo Zsolt Nagy | last post by:
My problem is about properties and the virtuality of the methods. I would like to create a property whose get and set methods are virtual. I had the same problems in Delphi before and the solution...
18
by: Robin Becker | last post by:
Is there a way to override a data property in the instance? Do I need to create another class with the property changed? -- Robin Becker
10
by: Jacob | last post by:
Is there a way to make a property of an inherited class invisible to the programmer? I know that using the keyword "new" allows you to create another property in the place of the existing one, but...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
3
by: Tom | last post by:
I am writing a Visual basic .Net database application. There are many forms that first let you select and look at a DB record and then when you click a "modify" button you are allowed to change...
14
by: emailscotta | last post by:
Some of the object properties in the Dojo Toolkit are set to objects but they are using syntax like this: object.property = new function() { this.property = someValue; this.property =...
9
by: Dustan | last post by:
Looking at this interactive session: def __init__(self, a): self.a = a def get_a(self): return self.__a def set_a(self, new_a): self.__a = new_a a = property(get_a, set_a) b =...
7
by: Gary | last post by:
Hi i'm exploring a tutorial stylesheet which uses the following code #header ul li a { .... line-height: 0.8em !important; line-height: 1em; .... }
8
by: active | last post by:
If I create a UserControl that inherits from, say, Panel, and I do not want my UserControl to have a property that it inherits from Panel. How can I remove it. For example, I might not want by...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.