A class is a blueprint for the object. Like function definitions begin with the keyword def
, in Python, we define a class using the keyword class
. The first string is called docstring
and has a brief description about the class. Although not mandatory, this is recommended.
>>> class My_Class:
'''This is a docstring.'''
pass
A class creates a new local namespace where all its attributes are defined. Attributes may be data or functions.
There are also special attributes in it that begins with double underscores (__)
. For example, __doc__
gives us the docstring of that class.
>>> class My_New_Class:
"This is my second class"
a = 36
def func(self):
print('Free Time Learning')
>>> print(My_New_Class.a)
36
>>> print(My_New_Class.func)
<function My_New_Class.func at 0x03798ED0>
>>> print(My_New_Class.__doc__)
This is my second class
>>>
Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute
Attribute | Description |
---|---|
__doc__ | Class documentation string or none, if undefined. |
__bases__ | A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list. |
__dict__ | Dictionary containing the class's namespace. |
__name__ | Class name. |
__module__ | Module name in which the class is defined. This attribute is "__main__" in interactive mode. |
>>> class Students:
'Common base class for all students'
stuCount = 0
def __init__(self, name, marks):
self.name = name
self.marks = marks
Students.stuCount += 1
def displayCount(self):
print ("Total Students %d" % Students.stuCount)
def displayStudents(self):
print ("Name : ", self.name, ", Marks: ", self.marks)
>>> print ("Students.__doc__:", Students.__doc__)
Students.__doc__: Common base class for all students
>>> print ("Students.__bases__:", Students.__bases__)
Students.__bases__: (<class 'object'>,)
>>> print ("Students.__dict__:", Students.__dict__)
Students.__dict__: {'__module__': '__main__', '__doc__': 'Common base class for all students', 'stuCount': 0,
'__init__': <function Students.__init__ at 0x03798F18>, '__dict__': <attribute '__dict__' of 'Students' objects>,
'__weakref__': <attribute '__weakref__' of 'Students' objects>}
>>> print ("Students.__name__:", Students.__name__)
Students.__name__: Students
>>> print ("Students.__module__:", Students.__module__)
Students.__module__: __main__
>>>
The python class object could be used to access different attributes.
It can also be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call.
This will create a new instance object named ob. We can access attributes of objects using the object name prefix.
This means to say, since MyClass.func
is a function object (attribute of class), ob.func will be a method object.
>>> class MyClass:
"This is my second class"
a = 10
def func(self):
print('FreeTimeLearn')
>>> ob = MyClass()
>>> print(MyClass.func)
<function MyClass.func at 0x03798E40>
>>> print(ob.func)
<bound method MyClass.func of <__main__.MyClass object at 0x0378BFB0>>
>>> ob.func()
FreeTimeLearn
>>>
There are two kind of operations class objects supports : attribute references and instantiation. Attribute references use the standard syntax, obj.name
for all attribute references in Python.
>>> class Student:
"""This is a simple class"""
stu_class = 'A'
stu_roll_no = 18
stu_name = "Suresh"
def messg(self):
return 'New Session will start soon.'
>>> Student.stu_class
'A'
>>> Student.stu_roll_no
18
>>> Student.stu_name
'Suresh'
>>> Student.messg
<function Student.messg at 0x037AB1E0>
>>> Student.__doc__
'This is a simple class'
>>>
__doc__
is also a valid attribute which returns the docstring
of the class.