programming container object
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Mon Dec 17 04:14:14 EST 2007
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.
I may have misunderstood what you are trying to do. Please feel free to
explain in more detail, perhaps with an example.
By the way, the correct way to get the result you want is to have
devlist.method() work as in your first example, and then do this:
s.extend(devlist.method())
--
Steven.
More information about the Python-list
mailing list