programming container object

Diez B. Roggisch deets at nospam.web.de
Mon Dec 17 05:21:23 EST 2007


Steven D'Aprano wrote:

> On Mon, 17 Dec 2007 18:18:11 +1100, bambam wrote:
> 
>> I wish to create a generic container object, devlist, such that
>> 
>>     devlist.method(arguments)
>> 
>> runs as
>> 
>>     for each dev in devlist.pool:
>>         dev.method(arguments)
>> 
>> and
>>     s = devlist.method(arguments)
>> 
>> runs as
>> 
>>     for each dev in devlist.pool:
>>         s.append(dev.method(arguments))
>> 
>> ...but it is outside my ability to do so.
>> 
>> Can anyone provide an example of how to do that?
> 
> 
> 
> If I've understood you correctly, I don't think it can be done.
> 
> It looks to me that you want:
> 
> s = instance.method()
> 
> and
> 
> instance.method()
> 
> to do completely different things. This is a bad idea and a recipe for
> confusion. In any case, it is not possible, because the instance method
> cannot know whether its result is being assigned to a name or just thrown
> away.

This isn't entirely correct - there _are_ ways to know.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/284742

Now if it is _desirable_ to use this as "clever" optimization scheme is a
subject worth discussing - I certainly wouldn't do it...

Diez



More information about the Python-list mailing list