Classes Are Objects Too

Q

Are classes objects in Python?

✍: FYIcenter.com

A

Yes, all classes are objects of "type" type in Python? You can verify this with the following Python code:

>>> class x:
...    pass
...
>>> type(x)
<class 'type'>

In other words, the "class x" statement block performed two activities:

  • Creates a new "type" object and hiding its statement block inside.
  • Introduced a variable "x" to reference to the "type" object.

Like any other types of objects, you can use the "dir()" to list its members:

>>> class x:
...    pass
...
>>> dir(x)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__form at__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
'__init__', '__init_s ubclass__', '__le__', '__lt__', '__module__', '__ne__', 
'__new__', '__reduce__',  '__reduce_ex__', '__repr__', '__setattr__', 
'__sizeof__', '__str__', '__subclas shook__', '__weakref__']

 

Defining and Using Class for New Data Types

⇒⇒Python Tutorials

2017-09-12, 116👍, 0💬