473,395 Members | 1,974 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,395 software developers and data experts.

property getter with more than 1 argument?

mk

It seems like getter is defined in such way that it passes only 'self':
class FunDict(dict):
def __init__(self):
self.fundict = dict()

def fget(self, fun):
return fundict[fun.func_name]

def fset(self, newfun):
self.fundict[newfun.func_name] = newfun

newfun = property (fget, fset)

>>a=FunDict()

a.newfun=f1

a.newfun('f1')
Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
a.newfun('f1')
TypeError: fget() takes exactly 2 arguments (1 given)

Is it possible to pass more than one argument to fget function?

I know: I can define a function with property name ('newfun' in the
example) and call it with more arguments. But then I do not get the
benefits of setter and property in general!

Jul 17 '08 #1
1 5169
On 17 juil, 16:57, mk <mrk...@gmail.comwrote:
It seems like getter is defined in such way that it passes only 'self':

class FunDict(dict):
def __init__(self):
self.fundict = dict()
What's the use of inheriting from dict here ???
def fget(self, fun):
wrong signature for a property.fget callback
return fundict[fun.func_name]
def fset(self, newfun):
self.fundict[newfun.func_name] = newfun

newfun = property (fget, fset)
>>a=FunDict()
>>>
>>a.newfun=f1
>>>
>>a.newfun('f1')
Note that you're passing a string, when your (incorrect) getter
expects a function object.
Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
a.newfun('f1')
TypeError: fget() takes exactly 2 arguments (1 given)

Is it possible to pass more than one argument to fget function?
Yes : call it directly !-)

Or remember the old saying:
"""
Any software problem can be solved by adding another layer of
indirection. Except, of course, the problem of too much indirection.
"""
Steve Bellovin of AT&T Labs

Applied to your problem, it would mean making your getter return a
closure that will take the func or func name and return the result of
the lookup, ie:

def fget(self):
return lambda funcname: self.fundict[funcname]
But anyway: this is still a typical case of arbitrary
overcomplexification. A plain dict would be enough. Or, given the
context (as exposed in an earlier post here), I'd suggest something
like:

class CallbacksRegister(object):
def __init__(self, **kw):
self._store = dict(**kw)
def register(self, func, name=None):
if name is None:
name = func.__name__
self._store(name) = func
return func

def get(self, name, default=None):
return self._store.get(name, default)

def __getattr__(self, name):
try:
return self._store[name]
except KeyError:
raise AttributeError('%s object has no attribute '%s'" %
(self, name)

callbacks = CallbacksRegister()

@callbacks.register
def f1(arg):
print "f1", arg

callbacks.f1("yadda")
callbacks.get('f1')('yadda')

I know: I can define a function with property name ('newfun' in the
example) and call it with more arguments. But then I do not get the
benefits of setter and property in general!
The benefit of computed attributes (the property class being only one
possible implementation) is to decouple interface (looks like an
ordinary attribute) from implementation (is in fact computed). And the
benefit from this decoupling is that you don't have to write getters/
setters until you really need them, since you can then turn a plain
attribute into a computed one without breaking the interface.

I fail to see what "benefits" you get from a property in your above
snippet.
Jul 18 '08 #2

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

Similar topics

3
by: kepes.krisztian | last post by:
Hi ! I want to create a property that can use parameter(s). In Delphi I can create same thing (exm: Canvas.Pixel -> Canvas.GetPixel(self,X,Y):integer; Canvas.SetPixel(self,X,Y,Color::integer); ...
13
by: Will Pittenger | last post by:
I have a Control derived class. When the parent of the control changes the control's Location property, the stack overflows. I have not found a way to find out what was on the stack when it does...
16
by: Flare | last post by:
Hi...Why canīt I access a property thorug property? Eg. class Foo { public SizeF MySize { get { return ms; } set { ms = value; } }}
8
by: Herve Bocuse | last post by:
Hi, I'm just wondering what are the guidelines for using a Property or a pair of get/set methods related to a member variable ? What do yu guys do ? Thank you Herve
4
by: Jimbo | last post by:
I am sort of new to C#. Currently have a private property called "_name" in a class. I have written a public getter and setter routine for it called "Name". Currently, the getter for the...
13
by: Peter Kirk | last post by:
Hi there, can someone tell me what exactly a "property" is in a C# class? As far as I can see it is "two methods" - ie a getter and a setter for an instance variable. What is the difference...
5
by: Brent Ritchie | last post by:
Hello, This is my first attempt at template programming so please bear with me. This is what I have so far: Property.h -------------------------- #ifndef PROPERTY_H_ #define PROPERTY_H_
7
by: Jaimi McEntire | last post by:
Given a property define like this: public decimal ControlAmount { get { return mControlAmount; } set { mControlAmount = value; } } I expected (under release mode) that when used as a...
0
by: Roy H. Han | last post by:
I don't understand what you're trying to do here. On Thu, Jul 17, 2008 at 10:57 AM, mk <mrkafk@gmail.comwrote:
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
0
BarryA
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...
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
Oralloy
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.