Python 为什么不是一门课?
我写这节课:Python 为什么不是一门课?,python,python-2.7,Python,Python 2.7,我写这节课: class Sensor: def __init__ (self,sensor_class_name, data): self.sensor_class_name = sensor_class_name self.data = data 当我进行此检查时: if type(Sensor) == type: print("type") else: print("not type") 它返回而不是t
class Sensor:
def __init__ (self,sensor_class_name, data):
self.sensor_class_name = sensor_class_name
self.data = data
当我进行此检查时:
if type(Sensor) == type:
print("type")
else:
print("not type")
它返回而不是type
当我对内置类执行此检查时,它返回type
为什么
传感器
不是类?在新样式的类中,从对象
类继承每个类对象
都是类型
类的实例
print isinstance(Sensor, type) # True
class Sensor:
def __init__ (self,sensor_class_name, data):
..
..
if type(Sensor) == type:
print("type")
else:
print("not type")
print isinstance(Sensor, type)
Ouput:-
>>>
not type
Flase
在新闻样式类中时
:-
class Sensor(object):
...
...
print isinstance(Sensor, type)
>>>
type
True
此外:-
In [52]: class A: pass
In [53]: type(A)
Out[53]: classobj
In [54]: class B(object):pass
In [55]: type(B)
Out[55]: type
In [56]: B.__class__
Out[56]: type
In [57]: A.__class__
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-57-5b54bd730948> in <module>()
----> 1 A.__class__
AttributeError: class A has no attribute '__class__'
[52]中的:A类:合格
In[53]:类型(A)
Out[53]:classobj
在[54]中:B类(对象):通过
In[55]:类型(B)
Out[55]:类型
在[56]中:B.\u类__
Out[56]:类型
在[57]中:一类__
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
---->1 A类__
AttributeError:类A没有属性“\uuuuu class\uuuuuuu”
在新样式的类中,以便从对象
类继承每个类对象
都是类型
类的实例
print isinstance(Sensor, type) # True
class Sensor:
def __init__ (self,sensor_class_name, data):
..
..
if type(Sensor) == type:
print("type")
else:
print("not type")
print isinstance(Sensor, type)
Ouput:-
>>>
not type
Flase
在新闻样式类中时
:-
class Sensor(object):
...
...
print isinstance(Sensor, type)
>>>
type
True
此外:-
In [52]: class A: pass
In [53]: type(A)
Out[53]: classobj
In [54]: class B(object):pass
In [55]: type(B)
Out[55]: type
In [56]: B.__class__
Out[56]: type
In [57]: A.__class__
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-57-5b54bd730948> in <module>()
----> 1 A.__class__
AttributeError: class A has no attribute '__class__'
[52]中的:A类:合格
In[53]:类型(A)
Out[53]:classobj
在[54]中:B类(对象):通过
In[55]:类型(B)
Out[55]:类型
在[56]中:B.\u类__
Out[56]:类型
在[57]中:一类__
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
---->1 A类__
AttributeError:类A没有属性“\uuuuu class\uuuuuuu”
在新样式的类中,以便从对象
类继承每个类对象
都是类型
类的实例
print isinstance(Sensor, type) # True
class Sensor:
def __init__ (self,sensor_class_name, data):
..
..
if type(Sensor) == type:
print("type")
else:
print("not type")
print isinstance(Sensor, type)
Ouput:-
>>>
not type
Flase
在新闻样式类中时
:-
class Sensor(object):
...
...
print isinstance(Sensor, type)
>>>
type
True
此外:-
In [52]: class A: pass
In [53]: type(A)
Out[53]: classobj
In [54]: class B(object):pass
In [55]: type(B)
Out[55]: type
In [56]: B.__class__
Out[56]: type
In [57]: A.__class__
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-57-5b54bd730948> in <module>()
----> 1 A.__class__
AttributeError: class A has no attribute '__class__'
[52]中的:A类:合格
In[53]:类型(A)
Out[53]:classobj
在[54]中:B类(对象):通过
In[55]:类型(B)
Out[55]:类型
在[56]中:B.\u类__
Out[56]:类型
在[57]中:一类__
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
---->1 A类__
AttributeError:类A没有属性“\uuuuu class\uuuuuuu”
在新样式的类中,以便从对象
类继承每个类对象
都是类型
类的实例
print isinstance(Sensor, type) # True
class Sensor:
def __init__ (self,sensor_class_name, data):
..
..
if type(Sensor) == type:
print("type")
else:
print("not type")
print isinstance(Sensor, type)
Ouput:-
>>>
not type
Flase
在新闻样式类中时
:-
class Sensor(object):
...
...
print isinstance(Sensor, type)
>>>
type
True
此外:-
In [52]: class A: pass
In [53]: type(A)
Out[53]: classobj
In [54]: class B(object):pass
In [55]: type(B)
Out[55]: type
In [56]: B.__class__
Out[56]: type
In [57]: A.__class__
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-57-5b54bd730948> in <module>()
----> 1 A.__class__
AttributeError: class A has no attribute '__class__'
[52]中的:A类:合格
In[53]:类型(A)
Out[53]:classobj
在[54]中:B类(对象):通过
In[55]:类型(B)
Out[55]:类型
在[56]中:B.\u类__
Out[56]:类型
在[57]中:一类__
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
---->1 A类__
AttributeError:类A没有属性“\uuuuu class\uuuuuuu”
>>导入类型
>>>目录(类型)
['BooleanType','BufferType','BuiltinFunctionType','BuiltinMethodType','ClassType','CodeType','ComplexType','DictProxyType','DictType','DictionaryType','EllipsStype','FileType','FloatType','FrameType','FunctionType','GeneratorType','GetSetDescriptorType','InstanceType','IntType','LambdaType','ListType','LongType','MemberDescriptoriptorType、MethodType、ModuleType、NoneType、NotImplementedType、ObjectType、SliceType、StringType、StringType、TracebackType、TupleType、TypeType、UnboundMethodType、UnicodeType、XRangeType、内置文件、文件名、包
>>>类别传感器:
... 通过
...
>>>如果类型(传感器)=types.ClassType:
... 打印“类型”
... 其他:
... 打印“不打印”
...
类型
>>>类型(类型)
>>>类型(传感器)
类型
本身就是类型
模块的类型
Class
是导入类型的类型
>>>目录(类型)
['BooleanType','BufferType','BuiltinFunctionType','BuiltinMethodType','ClassType','CodeType','ComplexType','DictProxyType','DictType','DictionaryType','EllipsStype','FileType','FloatType','FrameType','FunctionType','GeneratorType','GetSetDescriptorType','InstanceType','IntType','LambdaType','ListType','LongType','MemberDescriptoriptorType、MethodType、ModuleType、NoneType、NotImplementedType、ObjectType、SliceType、StringType、StringType、TracebackType、TupleType、TypeType、UnboundMethodType、UnicodeType、XRangeType、内置文件、文件名、包
>>>类别传感器:
... 通过
...
>>>如果类型(传感器)=types.ClassType:
... 打印“类型”
... 其他:
... 打印“不打印”
...
类型
>>>类型(类型)
>>>类型(传感器)
类型
本身就是类型
模块的类型
Class
是导入类型的类型
>>>目录(类型)
['BooleanType','BufferType','BuiltinFunctionType','BuiltinMethodType','ClassType','CodeType','ComplexType','DictProxyType','DictType','DictionaryType','EllipsStype','FileType','FloatType','FrameType','FunctionType','GeneratorType','GetSetDescriptorType','InstanceType','IntType','LambdaType','ListType','LongType','MemberDescriptoriptorType、MethodType、ModuleType、NoneType、NotImplementedType、ObjectType、SliceType、StringType、StringType、TracebackType、TupleType、TypeType、UnboundMethodType、UnicodeType、XRangeType、内置文件、文件名、包
>>>类别传感器:
... 通过
...
>>>如果类型(传感器)=types.ClassType:
... 打印“类型”
... 其他:
... 打印“不打印”
...
类型
>>>类型(类型)
>>>类型(传感器)
类型
本身就是类型
模块的类型
Class
是导入类型的类型
>>>目录(类型)
['BooleanType','BufferType','BuiltinFunctionType','BuiltinMethodType','ClassType','CodeType','ComplexType','DictProxyType','DictType','DictionaryType','EllipsStype','FileTyp