Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How can I define __getattr__ to operate on all items of container andpass arguments?

Reply
Thread Tools

How can I define __getattr__ to operate on all items of container andpass arguments?

 
 
Jeremy
Guest
Posts: n/a
 
      02-15-2011
I have a container object. It is quite frequent that I want to call a function on each item in the container. I would like to do this whenever I call a function on the container that doesn't exist, i.e., the container would return an attribute error.

For example

class Cont(object):
def __init__(self):
self.items = []

def contMethod(self, args):
print("I'm in contMethod.")

def __getattr__(self, name):
for I in self.items:
# How can I pass arguments to I.__dict__[name]?
I.__dict__[name]


>>> C = Cont()
>>> # Add some items to C
>>>C.contMethod()

I'm in contMethod.
>>>C.itemMethod('abc')

??????????


The trouble I'm getting into is that I can't pass arguments to the attributes in the contained item. In the example above, I can't pass 'abc' to the 'itemMethod' method of each item in the container.

Does someone know how I can accomplish this?

Thanks,
Jeremy

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-15-2011
On Tue, Feb 15, 2011 at 12:29 PM, Jeremy <(E-Mail Removed)> wrote:
> I have a container object. *It is quite frequent that I want to call a function on each item in the container. *I would like to do this whenever I call a function on the container that doesn't exist, i.e., the container would return an attribute error.


s/function/method/

> For example
>
> class Cont(object):
> * *def __init__(self):
> * * * *self.items = []
>
> * *def contMethod(self, args):
> * * * *print("I'm in contMethod.")
>
> * *def __getattr__(self, name):
> * * * *for I in self.items:
> * * * * * *# How can I pass arguments to I.__dict__[name]?
> * * * * * *I.__dict__[name]
>

<snip>
> The trouble I'm getting into is that I can't pass arguments to the attributes in the contained item. *In the example above, I can't pass 'abc' to the 'itemMethod' method of each item in the container.
>
> Does someone know how I can accomplish this?


Recall that:
x.y(z)
is basically equivalent to:
_a = x.y
_a(z)

So the arguments haven't yet been passed when __getattr__() is
invoked. Instead, you must return a function from __getattr__(); this
function will then get called with the arguments. Thus (untested):

def __getattr__(self, name):
def _multiplexed(*args, **kwargs):
return [getattr(item, name)(*args, **kwargs) for item in self.items]
return _multiplexed

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-15-2011
On Tue, 15 Feb 2011 12:29:36 -0800, Jeremy wrote:

> def __getattr__(self, name):
> for I in self.items:
> # How can I pass arguments to I.__dict__[name]?
> I.__dict__[name]


The same way you would pass arguments to any other function: with
function call syntax.

I.__dict__[name](arg1, arg2, arg3, ...)


--
Steven
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      02-15-2011
On 15/02/2011 20:29, Jeremy wrote:
> I have a container object. It is quite frequent that I want to call a function on each item in the container. I would like to do this whenever I call a function on the container that doesn't exist, i.e., the container would return an attribute error.
>
> For example
>
> class Cont(object):
> def __init__(self):
> self.items = []
>
> def contMethod(self, args):
> print("I'm in contMethod.")
>
> def __getattr__(self, name):
> for I in self.items:
> # How can I pass arguments to I.__dict__[name]?
> I.__dict__[name]
>
>
>>>> C = Cont()
>>>> # Add some items to C
>>>> C.contMethod()

> I'm in contMethod.
>>>> C.itemMethod('abc')

> ??????????
>
>
> The trouble I'm getting into is that I can't pass arguments to the attributes in the contained item. In the example above, I can't pass 'abc' to the 'itemMethod' method of each item in the container.
>
> Does someone know how I can accomplish this?
>

Try calling it. All you're currently doing is looking it up and then
discarding it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
is it ok to use c++ STL containers like string inside a structure andpass in message ques? ashjas C++ 5 02-05-2009 08:46 AM
How to delete and remove all items in a container<T*> James Aguilar C++ 8 03-22-2005 07:19 PM
maximum clock speed so that a design can safely operate benn VHDL 2 03-14-2005 06:47 AM
How does #define operate ? Timothy Madden C++ 5 09-28-2004 11:41 AM



Advertisments