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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: How do i instantiate a class_name passed via cmd line
Date Sun, 14 Feb 2016 14:48:16 +0100
Organization None
Lines 22
Message-ID <mailman.108.1455457712.22075.python-list@python.org> (permalink)
References <n9p0ba$s7b$1@dont-email.me> <diaid4F6assU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de kve7VjefZv234uDGXfs/Ew/LPzLoVUo+fE37BaihsduQ==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:How': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'gregory': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'strict': 0.22; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'skip:m 30': 0.27; 'expose': 0.29; 'convention': 0.30; 'class': 0.33; 'could': 0.35; 'instance': 0.35; 'something': 0.35; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'client': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'ebay': 0.84; 'subject:via': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd970e.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102916

Show key headers only | 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