Reputation: 1215
I write this class:
class Sensor:
def __init__ (self,sensor_class_name, data):
self.sensor_class_name = sensor_class_name
self.data = data
When I do this check:
if type(Sensor) == type:
print("type")
else:
print("not type")
It returns not type
.
When I do this check on built in class, it returns type
.
Why Sensor
is not a class?
Upvotes: 1
Views: 189
Reputation: 1215
I just inherit the class from object, and now it say it a class.
class Sensor (object):
def __init__ (self,sensor_class_name, data):
self.sensor_class_name = sensor_class_name
self.data = data
Upvotes: 0
Reputation: 169543
In ancient versions of Python (before v2.2) a class was a special thing (classobj
), separate from a type
:
>>> class OldStyleClass:
... pass
...
>>> type(OldStyleClass)
<type 'classobj'>
This is an "old style class"
Python 2.2 onwards you should generally always use "new style" classes, which are defined by inheriting from object
(or some subclass of it):
>>> class NewStyleClass(object):
... pass
...
>>> type(NewStyleClass)
<type 'type'>
There is a good post about the history of classes in Python, explaining the distinction - "New-style Classes" on The History of Python blog (written by the creator of Python)
In Python 3, this distinction was removed, so both of the class definitions above would be identical:
>>> class Blah:
... pass
...
>>> type(Blah)
<class 'type'>
>>>
>>> class Blah(object):
... pass
...
>>> type(Blah)
<class 'type'>
Upvotes: 0
Reputation: 5061
In new style classes so that inherite from object
class every class object
is instance
of type
class.
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
While in newstyle classes
:-
class Sensor(object):
...
...
print isinstance(Sensor, type)
>>>
type
True
also:-
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__'
Upvotes: 0
Reputation: 9874
Because types derive from objects:
In python2 classes without a parent class are type classobj. In python3 this changed and classes without parent class the inherit by default from object.
class A: pass
print type(A)
<type 'classobj'>
class B(object): pass
print type(B)
<type 'type'>
check this link for more info:https://wiki.python.org/moin/NewClassVsClassicClass
Upvotes: 0
Reputation: 95958
You are creating a class of the old (pre 2.2) Python using a plain class statement.
>>> type(Sensor)
<type 'classobj'>
Try:
>>> import types
>>> types.ClassType is type(Sensor)
True
A class
statement without bases creates a classic class. In order to create a new style class you have to specify object
as the base.
types.ClassType
resembles <type 'type'>
, instances of it are types.
Upvotes: 3
Reputation: 5275
>>> import types
>>> dir(types)
['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'CodeType', 'ComplexType', 'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType', 'FileType', 'FloatType', 'FrameType', 'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'InstanceType', 'IntType', 'LambdaType', 'ListType', 'LongType', 'MemberDescriptorType', 'MethodType', 'ModuleType', 'NoneType', 'NotImplementedType', 'ObjectType', 'SliceType', 'StringType', 'StringTypes', 'TracebackType', 'TupleType', 'TypeType', 'UnboundMethodType', 'UnicodeType', 'XRangeType', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
>>> class Sensor:
... pass
...
>>> if type(Sensor) == types.ClassType:
... print "type"
... else:
... print "not type"
...
type
>>> type(type)
<type 'type'>
>>> type(Sensor)
<type 'classobj'>
type
itself is type of types
module.
Class
is type of ClassType
.
Upvotes: 0