[英]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 为例:
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
? 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)
我什至找不到用isinstance(std_gen, something)
something
True
的东西
Edit: I found isinstance(std_gen, type)
returns True
but that barely makes sense in a docstring.编辑:我发现isinstance(std_gen, type)
返回True
但这在文档字符串中几乎没有意义。 What does that mean?这意味着什么?
Class Student is an instance of type 'type'. Class Student 是“type”类型的实例。 See metaclass for more information.有关更多信息,请参阅元类。 So type(Student) is 'type'.所以 type(Student) 是“类型”。 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.综上所述,s 是 Student 的实例,Student 是类型的实例,stu_gen 只是 Student 的别名。
I believe this is the solution that you're looking for.我相信这是您正在寻找的解决方案。
print(type(std_gen()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.