[Tutor] define vars by iteration

Luke Jordan luke.jordan at gmail.com
Thu Oct 20 16:46:54 CEST 2005

 It does help. I'm still stuck on part of it though, maybe a little
background will help.
 To learn more about classes I decided to make a class Map, with many
instances that have directions pointing to other Map instances. Each Map has
different objects you can find on it, but is still a Map, so I have to
customize each one.
 So if the only solution involves having names that could collide with
builtins, I'd like to do it that way. I recognize the potential for chaos,
but none of the names that I am using will collide (crossed fingers) becuase
I'm using names like "Backyard", "Kitchen" etc. So,
 >>> MyBackyard = Map("MyBackyard")
>>> MyBackyard.setDirections()
set direction for N: MyHouse
set direction for S: TheStreet
>>> MyBackyard.N
 etc, where MyHouse and TheStreet are made, via function, into MyHouse =
Map("MyHouse") and so forth. Each instance is pickled in its own file named
after self.name <http://self.name>.
  When I load the pickles like this
 classNameList = ["Yard","Kitchen","LivingRoom"]
 def getPickles():
 for name in classNameList:
 filepath = "C:\Python24\"+name+".dat"
 openedFile = file(filepath,"r")
 classInstanceName = pickle.load(openedFile)
 classInstanceName.name = classInstanceName
 print classInstanceName.name, classInstanceName
 Yard <class instance>
Kitchen <class instance>
LivingRoom <class instance>
>>> LivingRoom
<class instance>
>>> Kitchen
name Kitchen not defined
>>> Yard
name Yard not defined
 Also, I have done what I'm trying to do successfully by populating a
dictionary with class instances named after self.name <http://self.name>,
which, after the function runs, I can access this way
 >>> classDictionary["Yard"]
<class instance>
 the thing is I would like to be able to get at Yard's attributes by typing
 >>> Yard.anAttribute
garbage cans
 at the prompt rather than
 >>> Yard = classDictionary["Yard"]
>>> Yard.anAttribute
garbage cans

It's just that I can't pin down how to automate it, say through a
loadAllMaps() func.
 Thanks again for your helpful advice!
 On 10/19/05, Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
> On Wed, 19 Oct 2005, Luke Jordan wrote:
> > I've got a bunch of pickled class instances with
> > self.name <http://self.name><http://self.name/>attributes, and I would
> like to assign the
> > instances themselves to variables
> > named whatever is stored in self.name <http://self.name> <
> http://self.name/> using a function.
> > "Can't assign to literal", right? Is there a way to do this?
> Hi Luke,
> It's technically possible to do this, but discouraged. The reason it's
> not so safe is because some of those names might collide with your own
> program's names, or with the builtins. So if you start having pickled
> instances with names like 'list' or 'open', havoc is bound to ensue.
> Furthermore, Python variable names have some physical restrictions that
> may interfere with what you're trying to do:
> ######
> >>> 4meUshouldnthave = 42
> File "<stdin>", line 1
> 4meUshouldnthave = 42
> ^
> SyntaxError: invalid syntax
> ######
> (For the gory details on what's allowed in a name "identifier", see:
> http://www.python.org/doc/ref/identifiers.html)
> A safer way to do that I think you want is to use a separate dictionary
> container for those instances. As a concrete example:
> ######
> class Person:
> def __init__(self, name):
> self.name <http://self.name> = name
> people_names = ['fred', 'barney', 'wilma', 'betty']
> people = {}
> for name in people_names:
> people[name] = Person(name)
> ######
> Rather than using a direct variable name to refer to the person, we can go
> an indirect route, and refer to the corresponding entry in the 'people'
> dictionary. Does this make sense? This is safer because there's no
> potential to munge up the toplevel, plus the 'name' keys won't have the
> restrictions that Python variable names have.
> Hope this helps!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051020/60811317/attachment.html

More information about the Tutor mailing list