Newbie question on Classes

Erik Max Francis max at alcyone.com
Thu Jan 10 23:10:08 CET 2008


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 at alcyone.com && http://www.alcyone.com/max/
  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



More information about the Python-list mailing list