Hello all, sorry if this is a faq...
Problem: The intended effect is to override the method 'getattr' in a
way that i dont need to override the property explicitly too.
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr = property(fget=getattr)
class Derived(Base):
def getattr(self):
return 2*self._attr
if __name__ == "__main__":
b = Base(4)
d = Derived(4)
print b.attr, d.attr
output>> 4 8 ... so this does not work as i would like it to.
First solution: This is not what i want.
class Derived(Base):
def getattr(self):
return 2*self._attr
attr = property(fget=getattr)
Second solution: This is what i want, but...
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr = property(fget=lambda self: self.getattr())
class Derived(Base):
def getattr(self):
return 2*self._attr
Question: Isn't there an *alternative* way to do it without the lambda
function?
Thanks in advance! 6 1728
Pablo ha escrito: Hello all, sorry if this is a faq...
Problem: The intended effect is to override the method 'getattr' in a way that i dont need to override the property explicitly too.
class Base(object): def __init__(self, attr): self._attr = attr def getattr(self): return self._attr attr = property(fget=getattr)
class Derived(Base): def getattr(self): return 2*self._attr
if __name__ == "__main__": b = Base(4) d = Derived(4) print b.attr, d.attr
output>> 4 8 ... so this does not work as i would like it to.
sorry, i meant the following sentence:
output>> 4 4 ... so this does not work as i would like it to. First solution: This is not what i want.
class Derived(Base): def getattr(self): return 2*self._attr attr = property(fget=getattr)
Second solution: This is what i want, but...
class Base(object): def __init__(self, attr): self._attr = attr def getattr(self): return self._attr attr = property(fget=lambda self: self.getattr())
class Derived(Base): def getattr(self): return 2*self._attr
Question: Isn't there an *alternative* way to do it without the lambda function?
Thanks in advance!
Le Mardi 23 Mai 2006 15:55, Pablo a écrit*: Question: Isn't there an *alternative* way to do it without the lambda function?
No, it's good, why shouldn't you use a lambda function ?
Note you can do something like this :
class _virtualgetter :
def __init__(self, name) : self._n =name
def _call__(self, vself) : return getattr(vself, 'get' + self._n)()
def vprop(name) : return property(fget=_virtualgetter(name))
#anywhere
from ... import vprop
class Base(object):
def __init__(self, attr):
self._attr = attr
def getAttribute(self):
return self._attr
attr = vprop('Attribute')
class Derived(Base):
def getAttribute(self):
return 2*self._attr
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
The reason i would like a different approach to the lambda function is
just a question of personal taste... i dont really like it.
thanx!
Pablo wrote: Second solution: This is what i want, but...
class Base(object): def __init__(self, attr): self._attr = attr def getattr(self): return self._attr attr = property(fget=lambda self: self.getattr())
class Derived(Base): def getattr(self): return 2*self._attr
Question: Isn't there an *alternative* way to do it without the lambda function?
Thanks in advance!
Simplest:
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
@property # single-arg property is a read-only thing.
def attr(self):
return self.getattr()
### Little longer; maybe more explicit (tastes vary):
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
def attr(self):
return self.getattr()
attr = property(fget=attr)
--Scott David Daniels sc***********@acm.org
Oh! Thanx! Great! this is what i was looking for! :)
Scott David Daniels ha escrito: Pablo wrote:
Second solution: This is what i want, but...
class Base(object): def __init__(self, attr): self._attr = attr def getattr(self): return self._attr attr = property(fget=lambda self: self.getattr())
class Derived(Base): def getattr(self): return 2*self._attr
Question: Isn't there an *alternative* way to do it without the lambda function?
Thanks in advance!
Simplest:
class Base(object): def __init__(self, attr): self._attr = attr def getattr(self): return self._attr @property # single-arg property is a read-only thing. def attr(self): return self.getattr()
### Little longer; maybe more explicit (tastes vary):
class Base(object): def __init__(self, attr): self._attr = attr def getattr(self): return self._attr def attr(self): return self.getattr() attr = property(fget=attr)
--Scott David Daniels sc***********@acm.org
Le Mercredi 24 Mai 2006 22:37, Scott David Daniels a écrit*: * * *class Base(object): * * * * *def __init__(self, attr): * * * * * * *self._attr = attr * * * * *def getattr(self): * * * * * * *return self._attr * * * * *def attr(self): * * * * * * *return self.getattr() * * * * *attr = property(fget=attr)
but this has one drawback IMO, if you also want a virtual setter, you won'tbe
able to override both of them. This convention would be better to avoid
lambdas :
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
def __gattr(self): # double _ shows that this method is not virtual
return self.getattr()
def setattr(self,v):
self._attr = v
def __sattr(self,v):
return self.setattr(v)
attr = property(fget=__gattr, fset=__sattr)
But, this is too verbose for me, I would opt for the lambda syntax :).
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Oliver Fromme |
last post by:
Hi,
I'm trying to write a Python function that parses
an expression and builds a function tree from it
(recursively).
During parsing, lambda functions for the the terms
and sub-expressions...
|
by: Steven Bethard |
last post by:
I thought it might be useful to put the recent lambda threads into
perspective a bit. I was wondering what lambda gets used for in "real"
code, so I grepped my Python Lib directory. Here are some...
|
by: Tom Anderson |
last post by:
Comrades,
During our current discussion of the fate of functional constructs in
python, someone brought up Guido's bull on the matter:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
...
|
by: Xah Lee |
last post by:
Python, Lambda, and Guido van Rossum
Xah Lee, 2006-05-05
In this post, i'd like to deconstruct one of Guido's recent blog about
lambda in Python.
In Guido's blog written in 2006-02-10 at...
|
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,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| | |