module importing and variable syntax
Aahz Maruch
aahz at panix.com
Tue Mar 13 10:49:24 EST 2001
In article <3AADF3DC.1030003 at dynworks.com>,
Jeff Davis <jdavis at dynworks.com> wrote:
>
>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).
One of the most important Python rules is "explicit is better than
implicit". So if a person wants to register a generic class with your
module, provide a function for them to do it!
module A:
object = None
def register(newClass):
global object
object = newClass
module B:
import A
class Foo:
def square(self):
pass
A.register(Foo)
Module C:
import A
import B
o = A.object()
o.square()
--
--- Aahz <*> (Copyright 2001 by aahz at pobox.com)
Androgynous poly kinky vanilla queer het Pythonista http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6
Python:
Programming the way
Guido indented it
More information about the Python-list
mailing list