[Tutor] class Clowning: saving references to other instances

Rob McGee i812@iname.com
Mon, 17 Dec 2001 16:49:32 -0600

On Mon, Dec 17, 2001 at 03:46:22PM -0000, alan.gauld@bt.com wrote:
> >     self.location = location    # an integer, unique
> >     self.index = index          # also unique integer
> >     self.name = uppercase[index]  # uppercase letter, also 
> Huh? index is a unique integer... but you are taking 
> the uppercase of it?

I was just using that as a way of generating the name string, which has
to be unique. I probably don't need to save self.index, but figured I
should hang on to it in case I find a use later. :) Like for help in
iterating through a list, for example.

Oh, let me explain it. This was preceded by a
    from string import uppercase
statement, so "uppercase" is a string here, and I am slicing it with the
"index" (integer, 0-25) value.

> > from one Clown instance to another. The 
> > distance(self, other) method uses "exec" 
> > to store the distance values in self.(other.name) 
> Oh dear, not again... ;-)
> Using exec for this is really not very good.

No, not again. :) I think I've gotten away from exec, which was getting
to be my own personal GOTO. :)

> A dictionary to store these would be better.

I've already given up the exec. :) I will try a dictionary instead of a
list. Did you see my post about "dictionary vs. list considerations"
after this one? I was thinking that dictionaries would take up a lot
more memory. The one response I got indicated that speed of execution
would probably be better with dictionaries. But I was afraid that they
might hog a lot more memory?

> Correct, if you want to access things by a string key 
> (name in this case) the best solution is nearly 
> always to use a dictionary and nearly never to use 
> exec()!

ISTM if I switch to dictionaries I could use the objects themselves as
the keys? But I don't (yet) see how that would be easier than strings as
keys. The input will originate from the user, which of course means it
starts out as a string. I'll have to think about it some more.

> > The reason why I'm doing this is just to save 
> > a little redundant processing. 
> But you are introducing a heap of redundant 
> processing by calling exec!

#!/usr/bin/env python
def exec(*args):
  print "Don't use exec!!"

In all my *.py files. Does that look better? :)

> > One solution would be to put the "exec" code in a function 
> That tidies it up a little but doesn't really help.

Yes, I figured that out while I was writing the post to which you
replied. :)

> > Have I figured out my own solution?!? :) 
> Getting closer, but dictionaries instead of lists 
> would make it all much easier IMHO.

Yes, I thought so too. I'll try it.

Thanks for taking the time to understand the situation and reply.

And to you, Danny, thanks for the link. I looked it up. In this case I
don't have a security issue, but it's important to be aware of such
things. On the side, I was especially pleased to find a searchable
archive of this list! :)

    Rob - /dev/rob0