473,385 Members | 2,013 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,385 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 1234
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim Conkling | last post by:
I've run into a problem with a game I'm designing -- I (think) I need to do something ugly, for speed reasons, and I'm wondering if people here might have any ideas about how solve my problem more...
4
by: mihai | last post by:
I wander what is the penalty of speend for using object oriented programming (C++) against the play C. Have a nice day, Mihai
16
by: forester | last post by:
lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback). iterator is...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
6
by: lennon1 | last post by:
Hi, I have already started learning .NET and I have a question. If I want to do anything - Display Data, Navigate, Update - with database (SQL Server) in Visual Studio 2005, do I have to use all...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
2
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
46
by: Chris Stewart | last post by:
I've always had an interest in Python and would like to dabble in it further. I've worked on a few very small command line programs but nothing of any complexity. I'd like to build a really...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.