[英]What is the type of the object that instantiates classes in python?
我有一個我什至不知道如何搜索的問題。 以這個簡單的 class 為例:
class Student(object):
def _init_(self):
self.id = 0
def inc(self):
self.id += 1
std_gen = Student
std_gen
的類型是什么? 我試過了:
print(type(std_gen))
我得到了這個:
<class 'type'>
我需要找到它的類型並將其添加到文檔字符串中。 我什至找不到用isinstance(std_gen, something)
something
True
的東西
編輯:我發現isinstance(std_gen, type)
返回True
但這在文檔字符串中幾乎沒有意義。 這意味着什么?
Class Student 是“type”類型的實例。 有關更多信息,請參閱元類。 所以 type(Student) 是“類型”。 所以
s = Student()
std_gen = Student
type(s) // <class 'Student'>
type(std_gen) // <class 'type'>
綜上所述,s 是 Student 的實例,Student 是類型的實例,stu_gen 只是 Student 的別名。
我相信這是您正在尋找的解決方案。
print(type(std_gen()))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.