module importing and variable syntax
Jeff Davis
jdavis at dynworks.com
Tue Mar 13 05:18:04 EST 2001
I was previously a perl user, but now I am taking an interest in python.
I am used to common perl methods such as using "variable variables"
where the accessed variable depends on the situation (perl: "print
${$x};"). This is not especially important because that is essentially a
dictionary (in python terms), but combined with the module importing
(the way python does it is different than perl) I am having some problems.
Module importing doesn't take a string argument, but just a "bare word"
(in perl terms). This means I have to know the name of the file in
advance, and it needs to be in the right directory, or I need to use eval().
What I am trying to do is have an arbitrary string with an arbitrary
type (type with respect to my program, not python) access the function
associated with the string in the module associated with the type.
For example: someone writes an extension to my program creating a new
object of name "O". Then some other part of the program (i.e. another
extended module) tries to square an instance of that object, like:
o = O()
o.square()
However, my program is completely unaware of either "O" or "square"
until someone writes the module(s) and registers them with the program
(not writing to the main program's source).
It seems like I could get away with it if I used a lot of eval()
statements, but talk about unreadable code (especially since you have to
concatenate the variables in python to form a string)!
I think I am having problems understanding the way python works in this
regrard, which is why this question was probably way more complex than
it needed to be.
I would really appreciate some clarification. I like a lot about the way
python is made and I think it has a lot of potential. Right now the
concatenation of strings can be annoying compared to just (perl)
'$string = "a $anotherstring be";', but for a lot of programs it is well
worth the tradeoff.
Thanks,
Jeff Davis
More information about the Python-list
mailing list