Newbie question on Classes

A

Adrian Wood

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
 
N

Nanjundi

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
 
E

Erik Max Francis

Adrian said:
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.
 
S

Steven Clark

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.
"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."
 
J

John Machin

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:
.... 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?
 
N

Nanjundi

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.

"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top