Python Objects and Classes
In this tutorial, you will learn about the core functionality of Python objects
and classes. You'll learn what a class is, how to create it and use it in your
program.
Python Objects and Classes
Python is an object oriented programming language. Unlike procedure
oriented programming, where the main emphasis is on functions, object
oriented programming stresses on objects.
An object is simply a collection of data (variables) and methods (functions)
that act on those data. Similarly, a class is a blueprint for that object.
We can think of class as a sketch (prototype) of a house. It contains all the
details about the floors, doors, windows etc. Based on these descriptions
we build the house. House is the object.
As many houses can be made from a house's blueprint, we can create
many objects from a class. An object is also called an instance of a class
and the process of creating this object is called instantiation.
Defining a Class in Python
Like function definitions begin with the def keyword in Python, class
definitions begin with a class keyword.
The first string inside the class is called docstring and has a brief
description about the class. Although not mandatory, this is highly
recommended.
Here is a simple class definition.
class MyNewClass:
'''This is a docstring. I have created a new class'''
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.
As soon as we define a class, a new class object is created with the same
name. This class object allows us to access the different attributes as well
as to instantiate new objects of that class.
class Person:
"This is a person class"
age = 10
def greet(self):
print('Hello')
# Output: 10
print(Person.age)
# Output: <function Person.greet>
print(Person.greet)
# Output: 'This is my second class'
print(Person.__doc__)
Output
10
<function Person.greet at 0x7fc78c6e8160>
This is a person class