Search code examples
pythonclasstypesdocstring

What is the type of the object that instantiates classes in python?


I have a problem that I don't even know how to search for. Look at this simple class as an example:

class Student(object):
    def _init_(self):
        self.id = 0
    
    def inc(self):
        self.id += 1
 
std_gen = Student

What is the type of std_gen? I tried:

print(type(std_gen))

and I got this:

<class 'type'>

I need to find it's type and add it to a docstring. I can't even find something that returns True with isinstance(std_gen, something)

Edit: I found isinstance(std_gen, type) returns True but that barely makes sense in a docstring. What does that mean?


Solution

  • Class Student is an instance of type 'type'. See metaclass for more information. So type(Student) is 'type'. So

    s = Student()
    std_gen = Student
    type(s) // <class 'Student'>
    type(std_gen) // <class 'type'>
    

    To sum up, s is instance of Student, Student is instance of type and stu_gen is just alias of Student.