0

I have a class:

class Test():
    pass

I know the class name is "Test". How can I get class Test? A class is an object of class. I would like to get the class object based on its name, the text "Test".

In my project I have many classes defined. I would like to instantiate a class based on its name (without using an if statement).

martineau
  • 119,623
  • 25
  • 170
  • 301
david3000
  • 11
  • 1
  • 1
    "A class is an object of class" - that doesn't make sense – matszwecja May 18 '22 at 12:57
  • I was about to say the smae thing. what are you talking about!! – eshirvana May 18 '22 at 12:58
  • And what you are trying to do is a very weird requirement and against good programming principles. – matszwecja May 18 '22 at 13:01
  • `globals()['Test']` – cards May 18 '22 at 13:05
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – STerliakov May 18 '22 at 13:07
  • Where is the string `"Test"` coming from? You can assign the class itself to a variable (including passing it to a function directly) rather than assigning its name as text. (And if the string comes from user input, the user shouldn't know or care about internal details like type names.) – chepner May 18 '22 at 13:12
  • I think (maybe) what OP is trying to convey is that know that `Test` is an instance of `type` and a subclass of `object`. (Or maybe not.) – chepner May 18 '22 at 13:14
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 18 '22 at 13:41
  • Thanks, SUTerliakov. Your suggestion can work. I consider it as an option in case I couldn't find a better solution. I have many classes; I try to not fill a long dictionary. Thank you. – david3000 May 18 '22 at 15:31

2 Answers2

1

If the class is defined in the global namespace, you can do it this way:

class Test:
    pass


test_class = globals()["Test"]

print(test_class)  # -> <class '__main__.Test'>
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thank you, Martineau. Your method works great! Simple as well. I just found another way which I read many time and didn't understand (See below). But yours is better. – david3000 May 18 '22 at 14:24
  • https://stackoverflow.com/questions/4821104/dynamic-instantiation-from-string-name-of-a-class-in-dynamically-imported-module module = __import__(module_name) class_ = getattr(module, class_name) instance = class_() – david3000 May 18 '22 at 14:24
  • Indeed, that would be a way to get a class defined in another module (not quite the question you asked). – martineau May 18 '22 at 14:31
0

I don't suggest following such a convention, as this is very bad practice. A class is not an object of class, it's just a class that has been defined. Any objects you define using that class will be an object of that class and have its own instance. Please don't use the same name for different classes, this is almost never maintainable and you should never do it this way.

  • Thanks for the reply. In python everything id an object. For a class, it is an o – david3000 May 18 '22 at 13:18
  • All classes in Python are objects of the type class, and this type class is called Metaclass . Each class in Python, by default, inherits from the object base class. – david3000 May 18 '22 at 13:19
  • @david3000 But this is how Python works in the background, we are talking about actual code that we are using. – Some Intern Coder May 18 '22 at 13:26