471,342 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

programming container object

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?

Thanks,
Steve
Dec 17 '07 #1
5 1157
On Dec 17, 1:18 am, "bambam" <da...@asdf.asdfwrote:
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?

Thanks,
Steve
Ok, I'll take a stab at it.

-- Paul

class DevList(object):
def __init__(self, objs):
self.devpool = objs

def __getattribute__(self,attrname):
if attrname == "devpool":
return object.__getattribute__(self,attrname)
def ret(*args):
return [ getattr(p,attrname)(*args) for p in
self.devpool ]
return ret
dl = DevList([1,2,3])
print dl.__str__()

prints:

['1', '2', '3']
Dec 17 '07 #2
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.
Dec 17 '07 #3
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/Coo.../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
Dec 17 '07 #4
"Diez B. Roggisch" <de***@nospam.web.dewrites:
>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/Coo.../Recipe/284742
Not by using the public API, though. The above recipe relies on a
number of CPython's implementation details, and will stop working if
any of this changes. (I'm sure you're aware of that, but it might not
be obvious to some readers.)

It also doesn't work when the function is called from C code, or when
the unpacking is done in a separate step.
Dec 17 '07 #5
On Dec 17, 2:31 am, Paul McGuire <pt...@austin.rr.comwrote:
On Dec 17, 1:18 am, "bambam" <da...@asdf.asdfwrote:


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?
Thanks,
Steve

Ok, I'll take a stab at it.

-- Paul

class DevList(object):
def __init__(self, objs):
self.devpool = objs

def __getattribute__(self,attrname):
if attrname == "devpool":
return object.__getattribute__(self,attrname)
def ret(*args):
return [ getattr(p,attrname)(*args) for p in
self.devpool ]
return ret

dl = DevList([1,2,3])
print dl.__str__()

prints:

['1', '2', '3']- Hide quoted text -

- Show quoted text -
Here's some expanded demo code for the previously-posted DevList
class:

from math import sqrt

class NumberWrapper(object):
def __init__(self,value=0):
self.value = value

def inverse(self):
if self.value != 0:
return 1.0/self.value
else:
return None

def sqrt(self):
return sqrt(self.value)

def incrBy(self,offset):
self.value += offset
return self.value

dl = DevList([NumberWrapper(i) for i in range(5)])
print dl.sqrt()
print dl.inverse()
print dl.incrBy(10)
print dl.sqrt()
print dl.inverse()

prints:

[0.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0]
[None, 1.0, 0.5, 0.33333333333333331, 0.25]
[10, 11, 12, 13, 14]
[3.1622776601683795, 3.3166247903553998, 3.4641016151377544,
3.6055512754639891, 3.7416573867739413]
[0.10000000000000001, 0.090909090909090912, 0.083333333333333329,
0.076923076923076927, 0.071428571428571425]

-- Paul
Dec 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tim Conkling | last post: by
21 posts views Thread by Jon Slaughter | last post: by
47 posts views Thread by Thierry Chappuis | last post: by
2 posts views Thread by Daniel Lipovetsky | last post: by
46 posts views Thread by Chris Stewart | last post: by

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.