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 8816805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:42:10+00:00 2026-06-14T04:42:10+00:00

I have a string type with a value of MBSquareObject. MBSquareObject is class in

  • 0

I have a string type with a value of “MBSquareObject”. MBSquareObject is class in a file called MBObject. I want to import MBSquareObject dynamically.

If the square object was in a file of its own, this works:

__import__(type)

However, what I want to do is the equivalent of from MBObject import MBSquareObject. However, this doesn’t work:

from MBObject __import__(type)

How else could I do this?

Edit: the answers given are assuming that MBSquareObject is some sort of object on MBObject, but it’s just another class. MBSquareObject is a subclass of MBObject, so they are listed in the same file.

Edit: for some reason none of the answers are working. Here’s what I have:

# this is imported at the top of the file
from MBObject import MBObject
type = 'MBSquareObject'
__import__('MBObject', globals(), locals(), [type])
object_class = eval(type)
object = object_class()

Error: NameError: name ‘MBSquareObject’ is not defined

  • 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-14T04:42:11+00:00Added an answer on June 14, 2026 at 4:42 am

    Your example indicates that the module name MBObject doesn’t need to be accessed dynamically, only the object inside. In that case, you can just do

    import MBObject
    thing  = getattr(MBObject, type)
    

    Edit: One problem is that you are giving your module and class the same name, which makes it difficult to distinguish them in your code. You’re getting confused between classes and modules. You have two things called MBObject. One is a module, the other is a class inside that module. When you do from MBObject import MBObject, you import the class, but give yourself no reference to the module, making it awkward to subsequently import a second class (MBSquartObject) from the same module.

    You can get the effect you want by using the code I gave above, but you must not do from MBObject import MBObject — when you do that, you don’t give yourself a reference to the module, only the class in that module. Instead, just do import MBObject and then access the MBObject class via MBObject.MBObject.

    If you want to be able to refer to both the MBObject class and other classes from the same module without prefixing them with the module name, give the module a different name. Python style guidelines advise naming modules in all lowercase and classes in MixedCase. So name your module mbobject.py. Then you can do:

    import mbobject
    from mbobject import MBObject
    thing = getattr(mbobject, type)
    

    In general it is not a good idea in Python to give the same name to a module and a class. Modules and classes are different things, and giving them the same name can lead to confusions like this where you’re not clear on whether you’re dealing with the module or the class.

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

Sidebar

Related Questions

I have string value and Type of some variable (For example int and 32
I have a string of below type. [test1,3,(257,305,435,900)] Now i want to split this
I have a class class PCB { public: struct { string type; **linklist list;**//refer
I have a method that returns a list of type string. I want to
I have a string of type string xml = @<recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><daily dayFrequency=1 /></repeat><windowEnd>2012-10-31T10:00:00Z</windowEnd></rule></recurrence>; I want
I have a struct that stores the integer value as a custom string type.
I have wanted to use a HashMap that maps a value of type String
I have a string of type ishan,training I want to split the string after
Let's say I have a C# type : class MyType<T> { T Value {
I have JSON string like: '[{\type\: \EDITOR\, \value\: \fsddfsdsfdfs\}, {\type\: \CITA\, \value\: \Bug:\\n\\t\\t\\t\\t\\t0 open

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.