importing a class thru a variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john

    importing a class thru a variable?

    Hi,
    This is probably a question of questionable sanity, due to the fact I
    don't think I can explain this well. I'd like to have a script set up
    such that it imports a class that is named in the command line
    arguments as the first argument to the script.

    Let's say I have a script, command.py, and I'd like to run it like
    this:
    command.py class_id_like_t o_import --option1 value1 --option2 value2

    where 'class_id_like_ to_import' is a class name and --option1/value1
    and so on are arguments that get passed to that class.

    I know that trying to do something like:

    classname = sys.argv[1]
    import classname

    doesn't exactly work. I've tried to google for something like this,
    and search the old posts to this newsgroup and haven't quite found
    anything resembling what I'm looking for.

    Thanks in advance, and apologies if this has been answered before...
  • Chris Rebert

    #2
    Re: importing a class thru a variable?

    On Tue, Oct 21, 2008 at 4:07 PM, john <osborne6@gmail .comwrote:
    Hi,
    This is probably a question of questionable sanity, due to the fact I
    don't think I can explain this well. I'd like to have a script set up
    such that it imports a class that is named in the command line
    arguments as the first argument to the script.
    >
    Let's say I have a script, command.py, and I'd like to run it like
    this:
    command.py class_id_like_t o_import --option1 value1 --option2 value2
    >
    where 'class_id_like_ to_import' is a class name and --option1/value1
    and so on are arguments that get passed to that class.
    >
    I know that trying to do something like:
    >
    classname = sys.argv[1]
    import classname
    Python import modules, not classes, but I believe you're looking for
    the __import__() function.
    See the first entry on http://www.python.org/doc/2.5.2/lib/built-in-funcs.html

    Cheers,
    Chris
    --
    Follow the path of the Iguana...

    >
    doesn't exactly work. I've tried to google for something like this,
    and search the old posts to this newsgroup and haven't quite found
    anything resembling what I'm looking for.
    >
    Thanks in advance, and apologies if this has been answered before...
    --

    >

    Comment

    • Aaron Brady

      #3
      Re: importing a class thru a variable?

      On Oct 21, 6:07 pm, john <osbor...@gmail .comwrote:
      Hi,
      This is probably a question of questionable sanity, due to the fact I
      A question of questionable? Unacceptable. None of those, please.

      Yes, __import__ will do dynamics.

      Check your terminology. 'arguments that get passed to the class'
      doesn't evaluate right; classes don't have arguments. You might mean
      'import a module', but modules don't take arguments either. You might
      mean 'import a factory function', which returns a class, which the
      language can do, but it didn't sound like it.

      Comment

      Working...