'Dynamic' class instances

Ken Power iam at not.you
Wed Oct 27 11:56:46 EDT 1999


Tue, 26 Oct 1999 18:22:52 -0400, in comp.lang.python, Eric Jacobs
<x at x.x> managed to squeak:

>skaller wrote:
>
>> if you follow my strange thinking, what I am attempting is assign a
>> class instance, not to test, but to the variable defined(??) by test.
>> If apple was given at the class name: prompt, I would then have a
>> class instance named apple, not test.
>                                  ^^^^
>
>It seems the original poster was wondering how to create a variable
>in the namespace when the name of that variable was not known at
>compile time. That then would be:
>
>        exec test + " = spam()"
>
>Although exec is a pretty nasty way to do that. If I wanted it in
>the global namespace, I would do:
>
>        globals()[test] = spam()
>
>locals() does not work in an analogous manner; you'd have to use
>exec to create a variable in the local namespace.
>
>But more than anything, I'd recommend using a dictionary for the
>purpose rather than manipulating any namespaces.

Hmmm. Thank you for the input. I really appreciate it. If you would
please, in a simple example, how would you, "use a  dictionary for the
purpose rather than manipulating any namespaces."?

The part about namespaces I understand (should the new instance be
global in nature, or localized to a module), using dictionaries in
your implied sense, I do not understand, please expound.

Ken
--------------------------------
Ken Power
uncle_wiggly at bigfoot dot com
get that?
--------------------------------




More information about the Python-list mailing list