Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102916

Re: How do i instantiate a class_name passed via cmd line

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: How do i instantiate a class_name passed via cmd line
Date 2016-02-14 14:48 +0100
Organization None
Message-ID <mailman.108.1455457712.22075.python-list@python.org> (permalink)
References <n9p0ba$s7b$1@dont-email.me> <diaid4F6assU1@mid.individual.net>

Show all headers | View raw


Gregory Ewing wrote:

> Something like this should do it:
> 
>    instance = getattr(module, class_name)(module_name, product)
> 
> If the class name is always the same as the module name with the
> first letter capitalized, you could use
> 
>    instance = getattr(module, module_name.capitalize())(module_name,
>    product)

If the convention is that strict you could also expose the class under an 
alias:

# in ebay.py
Site = Ebay

# in the client module
module = importlib.import_module(module_name)
instance = module.Site(product) 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How do i instantiate a class_name passed via cmd line "Veek. M" <vek.m1234@gmail.com> - 2016-02-14 10:10 +0530
  Re: How do i instantiate a class_name passed via cmd line Rick Johnson <rantingrickjohnson@gmail.com> - 2016-02-13 21:31 -0800
    Re: How do i instantiate a class_name passed via cmd line "Veek. M" <vek.m1234@gmail.com> - 2016-02-14 11:09 +0530
      Re: How do i instantiate a class_name passed via cmd line Rick Johnson <rantingrickjohnson@gmail.com> - 2016-02-13 21:55 -0800
  Re: How do i instantiate a class_name passed via cmd line Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-02-14 18:41 +1300
    Re: How do i instantiate a class_name passed via cmd line "Veek. M" <vek.m1234@gmail.com> - 2016-02-14 11:21 +0530
    Re: How do i instantiate a class_name passed via cmd line Peter Otten <__peter__@web.de> - 2016-02-14 14:48 +0100
  Re: How do i instantiate a class_name passed via cmd line Cameron Simpson <cs@zip.com.au> - 2016-02-14 16:20 +1100

csiph-web