Create an instance, I already have a type

class ClassA: pass mytype = type(ClassA) 

Using mytype, how do you create an instance of ClassA?

Note that I am NOT looking for this as my answer :)

 instance = ClassA() 

I know that in this example, everything you need to do, but suppose you have this type (type), you know nothing about it, and you need to instantiate it.

I tried just calling mytype () and it returns an error stating that type () takes 1 or 3 arguments. I assume that these arguments can be associated with any arguments that you want to pass to the init method of the object being created.

Note. I saw a few questions about how to create an object, given the string representing its full name. Please note that here I already have a type, so is it a little different? I think?

EDIT ... I was hoping I could make the equivalent of the following .Net code:

 class A {} class Program { static void Main(string[] args) { Type type = typeof(A); // Given the type, I can create a new instance object new_instance = Activator.CreateInstance(type); } } 

I assumed that in Python the type (ClassA) gave something back that I could subsequently use to create an instance of ClassA. It looks like I misunderstood the type in python.

+6
source share
3 answers

If you use a new style class, this will work:

 class ClassA(object): pass o = ClassA() mytype = type(o) new_object = mytype() 
+14
source

This question does not make sense. type(X) indicates type X , and the return value does not contain information about the spesific class X , in the same way type("abc") , which is str , does not contain information about the string used as an argument.

If class X has its own metaclass, the answer will always be type , since this is the default metaclass in python. In your case, then mytype is type , that is, they are the same object.

You can use type to check the type of an object if you use a single argument, or using three arguments that you can create a class dynamically:

 myclass = type('ClassB', (object,), {"class_attribute": 3.14}) 

Suppose for a moment that mytype worked the way you thought. Remember that an instance of a type is a class, just like an instance of a class is an object. Thus, in the same way (except for singleton classes) ClassA() is not ClassA() (each instance is unique), mytype() is not mytype() - that is, you must get a different class every time you create an instance of the type. That way you still won't get ClassA . To illustrate the point with code, you can run:

 >>> type('A', (object,), {}) == type('A', (object,), {}) False 

CANCELING ANSWERS :

Classes in python are "first class citizens", unlike what you have in most compiled languages. This means that you can pass the class directly; you don't need the typeof equivalent:

 >>> class A(object): pass ... >>> B = A >>> B() <__main__.A object at 0xb781138c> >>> def print_class_name(cls): ... print cls.__name__ ... >>> print_class_name(B) A 

This article provides an excellent explanation of how type , object and object instances are related in python.

+4
source

Lysir says this is not possible with type . Maybe something like

 class ClassA: pass myclass = ClassA instance = myclass() 
+3
source

Source: https://habr.com/ru/post/903325/


All Articles