Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 71049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:49:36+00:00 2026-05-10T19:49:36+00:00

I’m working on a web application that will return a variable set of modules

  • 0

I’m working on a web application that will return a variable set of modules depending on user input. Each module is a Python class with a constructor that accepts a single parameter and has an ‘.html’ property that contains the output.

Pulling the class dynamically from the global namespace works:

result = globals()[classname](param).html 

And it’s certainly more succinct than:

if classname == 'Foo':     result = Foo(param).html elif classname == 'Bar':     ... 

What is considered the best way to write this, stylistically? Are there risks or reasons not to use the global namespace?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:49:37+00:00Added an answer on May 10, 2026 at 7:49 pm

    A flaw with this approach is that it may give the user the ability to to more than you want them to. They can call any single-parameter function in that namespace just by providing the name. You can help guard against this with a few checks (eg. isinstance(SomeBaseClass, theClass), but its probably better to avoid this approach. Another disadvantage is that it constrains your class placement. If you end up with dozens of such classes and decide to group them into modules, your lookup code will stop working.

    You have several alternative options:

    1. Create an explicit mapping:

       class_lookup = {'Class1' : Class1, ... }  ...  result = class_lookup[className](param).html 

      though this has the disadvantage that you have to re-list all the classes.

    2. Nest the classes in an enclosing scope. Eg. define them within their own module, or within an outer class:

      class Namespace(object):     class Class1(object):         ...     class Class2(object):         ... ... result = getattr(Namespace, className)(param).html 

      You do inadvertantly expose a couple of additional class variables here though (__bases__, __getattribute__ etc) – probably not exploitable, but not perfect.

    3. Construct a lookup dict from the subclass tree. Make all your classes inherit from a single baseclass. When all classes have been created, examine all baseclasses and populate a dict from them. This has the advantage that you can define your classes anywhere (eg. in seperate modules), and so long as you create the registry after all are created, you will find them.

      def register_subclasses(base):     d={}     for cls in base.__subclasses__():         d[cls.__name__] = cls         d.update(register_subclasses(cls))     return d  class_lookup = register_subclasses(MyBaseClass) 

      A more advanced variation on the above is to use self-registering classes – create a metaclass than automatically registers any created classes in a dict. This is probably overkill for this case – its useful in some ‘user-plugins’ scenarios though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 87k
  • Answers 87k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should try using passive mode for FTP. The WebClient… May 11, 2026 at 5:36 pm
  • Editorial Team
    Editorial Team added an answer For my own use, I have created some kind of… May 11, 2026 at 5:36 pm
  • Editorial Team
    Editorial Team added an answer Craig Strong has a pretty nice article about MVC in… May 11, 2026 at 5:36 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.