Finding the instance reference of an object

Terry Reedy tjreedy at udel.edu
Thu Oct 16 22:39:25 EDT 2008


Astley Le Jasper wrote:

> I'm creating mulitple instances, putting them in a list, iterating
> through the list to send them to some functions where process them
> with some instance specific parameters. Something along the lines of:
> 
> bob = someobject()
> harry = someobject()
> fred = someobject()
> 
> parameterdict = {'bob':(0,1,2),'harry':(3,4,5),'fred':(6,7,8)}
> people_list = (bob, harry, fred)
> 
> for person in people_list:
>   add_parameters(person)
> 
> def add_parameters(person)
>   mytuple = parameterdict[??????instance.name????]
>   person.x = mytuple[0]
>   person.y = mytuple[1]
>   person.z = mytuple[2]

If you want an object to have a 'personal' name, give it a name 
attribute, just like python does to modules, classes, and functions.

class someob():
   def __init__(self,name):
     self.name = name

people = {someob('bob'), someob('harry'), someob('fred')
...
def add_param(person):
   mytuple = parameterdict[person.name]
   ...




More information about the Python-list mailing list