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

  • SEARCH
  • Home
  • 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 7997233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:55:31+00:00 2026-06-04T14:55:31+00:00

I have the following setup in one module: class A(object): # stuff class B(object):

  • 0

I have the following setup in one module:

class A(object):
    # stuff

class B(object):
    # stuff

Now what I want to do is, creating an instance of class A by name (I just have the class name as a string) inside of B. How can I do this avoiding the globals function?

  • 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. Editorial Team
    Editorial Team
    2026-06-04T14:55:32+00:00Added an answer on June 4, 2026 at 2:55 pm

    Why not just use A? Or do you have just a string 'A'? If yes, globals()['A'] is the way to go. The alternative would be getattr(sys.modules[__name__], 'A') but obviously globals() is more appropriate.

    >>> dis.dis(lambda: getattr(sys.modules[__name__], 'Foo'))
      1           0 LOAD_GLOBAL              0 (getattr)
                  3 LOAD_GLOBAL              1 (sys)
                  6 LOAD_ATTR                2 (modules)
                  9 LOAD_GLOBAL              3 (__name__)
                 12 BINARY_SUBSCR
                 13 LOAD_CONST               1 ('Foo')
                 16 CALL_FUNCTION            2
                 19 RETURN_VALUE
    
    >>> dis.dis(lambda: globals()['Foo'])
      1           0 LOAD_GLOBAL              0 (globals)
                  3 CALL_FUNCTION            0
                  6 LOAD_CONST               1 ('Foo')
                  9 BINARY_SUBSCR
                 10 RETURN_VALUE
    
    >>> dis.dis(lambda: Foo)
      1           0 LOAD_GLOBAL              0 (Foo)
                  3 RETURN_VALUE
    

    So by just looking at the instructions used for the various ways to access Foo, using globals() is most likely faster than going through sys.modules.

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

Sidebar

Related Questions

I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have following setup A -> B -> C A-Mapping: <hibernate-mapping> <class name=db.base.A table=A>
I have the following setup: class.staff.php This defines many variables, the one I'm working
I have the following setup. Class, say, Car that has a CarPart (belongsTo=[car:Car]). When
I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
Let's assume we have following models: from django.db import models class Foo(models.Model): name =
I have the following Setup: One service called CoreHost should receive a ExecuteWorkflowByAttributeCommand which
So let's say I have the following setup in Google Reader Folder One Engadget
I have following setup, but when I put 1024 and replace all 512 with
I have the following setup, and I need to know how to persist state.

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.