[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Newbie question on Classes

Adrian Wood

1/10/2008 9:47:00 PM

Hi al! I'm new to the list, and reasonably new to Python, so be gentle.

Long story short, I'm having a hard time finding a way to call a
function on every object of a class at once. Example:

I have a class Person, which has a function state(). This prints a
basic string about the Person (position, for example). In the program,
I have created two objects of class Person, called man and woman.

I can call man.state() and then woman.state() or Person.state(man) and
Person.state(woman) to print the status of each. This takes time and
space however, and becomes unmanageable if we start talking about a
large number of objects, and unworkable if there is an unknown number.
What I'm after is a way to call the status of every instance of Man,
without knowing their exact names or number.

I've gone through the relevant parts of the online docs, tried to find
information elsewhere online, and looked for code samples, but the
ionformation either isn't there, or just isn't clicking with me. I've
tried tracking the names of each object in a list, and even creating
each object within a list, but don't seem to be able to find the right
syntax to make it all work.

I'd appreciate anyone who could help, especially if they could include
a short sample. My apologies if I'm not following the etiquette of the
group in some way my making this request.

Thank you,
Adrian
5 Answers

Nanjundi

1/10/2008 10:07:00 PM

0

On Jan 10, 4:46 pm, "Adrian Wood" <aaw...@gmail.com> wrote:
> Hi al! I'm new to the list, and reasonably new to Python, so be gentle.
>
> Long story short, I'm having a hard time finding a way to call a
> function on every object of a class at once. Example:
>
> I have a class Person, which has a function state(). This prints a
> basic string about the Person (position, for example). In the program,
> I have created two objects of class Person, called man and woman.
>
> I can call man.state() and then woman.state() or Person.state(man) and
> Person.state(woman) to print the status of each. This takes time and
> space however, and becomes unmanageable if we start talking about a
> large number of objects, and unworkable if there is an unknown number.
> What I'm after is a way to call the status of every instance of Man,
> without knowing their exact names or number.
>
> I've gone through the relevant parts of the online docs, tried to find
> information elsewhere online, and looked for code samples, but the
> ionformation either isn't there, or just isn't clicking with me. I've
> tried tracking the names of each object in a list, and even creating
> each object within a list, but don't seem to be able to find the right
> syntax to make it all work.
>
> I'd appreciate anyone who could help, especially if they could include
> a short sample. My apologies if I'm not following the etiquette of the
> group in some way my making this request.
>
> Thank you,
> Adrian

Hi Adrian,
One easy way, is to append the objects to a list, as you have
mentioned and call the state method in iteration.
l = []
l.append(man)
l.append(woman)

# Print the state.
for item in l:
print item.state()

(If I understood right, man and woman qualifies as "every instance of
man")
-N

Erik Max Francis

1/10/2008 10:10:00 PM

0

Adrian Wood wrote:

> I can call man.state() and then woman.state() or Person.state(man) and
> Person.state(woman) to print the status of each. This takes time and
> space however, and becomes unmanageable if we start talking about a
> large number of objects, and unworkable if there is an unknown number.
> What I'm after is a way to call the status of every instance of Man,
> without knowing their exact names or number.
>
> I've gone through the relevant parts of the online docs, tried to find
> information elsewhere online, and looked for code samples, but the
> ionformation either isn't there, or just isn't clicking with me. I've
> tried tracking the names of each object in a list, and even creating
> each object within a list, but don't seem to be able to find the right
> syntax to make it all work.

You'll have to retain a list of all extant Person objects, and then a
function which calls the relevant method on every element of that list
and returns the result however you wish. You can easily encapsulate
that list into a class attribute of Person, and the function into a
static method of the class for elegance, but it's not required to get
the proper functionality.

--
Erik Max Francis && max@alcyone.com && http://www.alcyon...
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
Life is a gamble so I should / Live life more carefully
-- TLC

Steven Clark

1/10/2008 10:32:00 PM

0

> l = []
> l.append(man)
> l.append(woman)
>
> # Print the state.
> for item in l:
> print item.state()
>
>
Small, off-topic nitpick:
please don't use "l" (lower-case el) as a variable name.

>From http://www.python.org/dev/peps...:
"Naming Conventions

Names to Avoid

Never use the characters `l' (lowercase letter el), `O' (uppercase
letter oh), or `I' (uppercase letter eye) as single character variable
names.

In some fonts, these characters are indistinguishable from the numerals
one and zero. When tempted to use `l', use `L' instead."

John Machin

1/10/2008 10:39:00 PM

0

On Jan 11, 8:46 am, "Adrian Wood" <aaw...@gmail.com> wrote:
> Hi al! I'm new to the list, and reasonably new to Python, so be gentle.
>
> Long story short, I'm having a hard time finding a way to call a
> function on every object of a class at once.

A class is a factory that creates objects. It keeps no records of what
it has created -- it will never have to recall them for remediation.
The customer needs to keep track of them.

[snip]

>I've
> tried tracking the names of each object in a list

In general, objects have 0, 1, or many "names" ... "tracking the
names" is not a very meaningful concept hence not a very useful
concept.

At your application level, a person's name is what they call
themselves and can vary over time and with the purpose for which it is
used, and what is actually recorded in databases is subject to a non-
trivial error rate -- hence using person's name as a key is not a very
good idea.

> and even creating
> each object within a list, but don't seem to be able to find the right
> syntax to make it all work.

"creating each object in a list" sounds like it's going down the right
path -- you need to keep a collection of objects somehow if you want
to perform some operations on all objects, and a list is a reasonable
start.

Here's a very basic simple skeleton toy demonstration:

>>> class Person(object):
.... def __init__(self, name, hair):
.... self.name = name
.... self.hair = hair
.... def show(self):
.... print 'name=%r hair=%r' % (self.name, self.hair)
....
>>> plist = []
>>> obj1 = Person(name='Bluey', hair='red')
>>> plist.append(obj1)
>>> obj2 = Person(name='John', hair='grey')
>>> plist.append(obj2)
>>>
>>> for obj in plist:
.... obj.show()
....
name='Bluey' hair='red'
name='John' hair='grey'
>>>

Does this help?

Nanjundi

1/11/2008 7:03:00 PM

0

On Jan 10, 5:32 pm, "Steven Clark" <steven.p.cl...@gmail.com> wrote:
> > l = []
> > l.append(man)
> > l.append(woman)
>
> > # Print the state.
> > for item in l:
> > print item.state()
>
> Small, off-topic nitpick:
> please don't use "l" (lower-case el) as a variable name.
>
> >Fromhttp://www.python.org/dev/peps...:
>
> "Naming Conventions
>
> Names to Avoid
>
> Never use the characters `l' (lowercase letter el), `O' (uppercase
> letter oh), or `I' (uppercase letter eye) as single character variable
> names.
>
> In some fonts, these characters are indistinguishable from the numerals
> one and zero. When tempted to use `l', use `L' instead."

Thanks for the PEP, Steven.
-N