[Tutor] __cmp__() method examples?

alan.gauld@bt.com alan.gauld@bt.com
Thu, 6 Dec 2001 14:19:21 -0000


> from string import uppercase
> for letter in uppercase:
>   execStr = letter + ' = Project("' + letter + '")'
>   # example -- 'A = Project("A")', 'Z = Project("Z")
>   exec(execStr)
> 
> (Sorry about the exec's, but I really don't know another way 

How about:

letters = {}
letters[letter] = Project(letter)

That's what the exec is doing for you under the hood 
- adding a new variable A,B,C etc to the global 
namespace dictionary. 

Now you can access your objects as:

print letter["A"]

etc.

> I want. I want these instances in the global namespace, and I need to
> know exactly what each one is named. 

Any good reason why you'd want them global?

Alan G