告别命令行恐惧:用VScode的Remote-SSH插件,像操作本地文件一样玩转远程服务器
2026/6/2 18:16:00
print(type(123))print(type(3.14))print(type("hello"))print(type([1,2,3]))print(type({"a":1}))# 输出结果 <class 'int'> <class 'float'> <class 'str'> <class 'list'> <class 'dict'>classMyClass:passobj=MyClass()print(type(obj))# 输出结果 <class '__main__.MyClass'>result=type(123)print(result)print(type(result))# 输出结果 <class 'int'> <class 'type'>__name__属性获取类型名称result=type(123)print(result)print(result.__name__)# 输出结果 <class 'int'> intdefcheck_type(obj):iftype(obj)==int:return"int"eliftype(obj)==float:return"float"eliftype(obj)==str:return"str"eliftype(obj)==list:return"list"eliftype(obj)==dict:return"dict"else:return"unknown"print(check_type(123))print(check_type(3.14))print(check_type("hello"))print(check_type([1,2,3]))print(check_type({"a":1}))# 输出结果 int float str list dictclassParent:passclassChild(Parent):passobj=Child()print(type(obj)==Child)print(type(obj)==Parent)print(isinstance(obj,Child))print(isinstance(obj,Parent))# 输出结果 True False True TrueMyClass=type('MyClass',(),{'x':42})obj=MyClass()print(obj.x)# 输出结果 42classBase:defshow(self):return"base class"Child=type('Child',(Base,),{'value':100})c=Child()print(c.show())print(c.value)# 输出结果 base class 100defsay_hello(self):returnf"hello{self.name}"Person=type('Person',(),{'__init__':lambdaself,name:setattr(self,'name',name),'greet':say_hello})p=Person("tom")print(p.greet())# 输出结果 hello tom