Python – Class and Objects
Class and Objects are basic building blocks of Python. It is also a core concept in a style of programming known as Object Oriented Programming (or OOP). Classes allow programmers to specify the structure of an object (i.e. its attributes or fields, etc.) and the its behavior separately from the objects themselves.
Instances or Objects are created from class. All instances/objects of a class possess the same data variables but contain their own data values.
In Python everything is an object, such as an integers are an example of the int class, real numbers are examples of the float class etc. For example,
print(type(3)) print(type(3.3)) print(type(True)) print(type('TalksInfo')) print(type([1, 2, 3, 4]))
Above code snippet will give output as below,
<class 'int'> <class 'float'> <class 'bool'> <class 'str'> <class 'list'>
Python not only uses built-in classes. It is also possible to define user defined types (classes). It can be used to create your own data structures, your own data types, your own applications etc.
What Are Class and Objects for in Python?
In Python classes are used:
- as a template to create instances (or objects) of that class,
- define instance methods or common behavior for a class of objects,
- define attributes or fields to hold data within the objects,
- be sent messages.
Objects (or instances), on the other hand, can:
- be created from a class,
- hold their own values for instance variables,
- be sent messages,
- execute instance methods,
- may have many copies in the system (all with their own data).
In Python, a class definition has the following format,
class nameOfClass(SuperClass): __init__ attributes methods
- Class – A class defines a combination of data and behaviour that operates on that data. A class acts as a template when creating new instances.
- Instance or object – An instance also known as an object is an example of a class. All instances of a class possess the same data fields/attributes but contain their own data values. Each instance of a class responds to the same set of requests.
- Attribute/field/instance variable – The data held by an object is represented by its attributes (also sometimes known as a field or an instance variable). The “state” of an object at any particular moment relates to the current values held by its attributes.
- Method – A method is a procedure defined within an object.
- Message – A message is sent to an object requesting some operation to be performed or some attribute to be accessed.
Although you should note that you can mix the order of the definition of attributes, and methods as required within a single class.
The following code is an example of a class definition:
class Person: def __init__(self, name, age): self.name = name self.age = age
It is very common to define a class in a file named after that class. This makes it easier to find the code associated with a class. This is
shown below using the PyCharm IDE:
- The Person class possesses two attributes (or instance variables) called name and age.
- There is also a special method defined called __init__. This is an initializer (also known as a constructor) for the class.
- As Name and an age is supplied with constructor, it must be passed when an instance of the Person class is created.
- The parameters to the __init__ method are local variables and will disappear when the method terminates, but self.name and self.age are instance variables and will exist for as long as the object is available.
Creating Objects of the Class Person
The following creates two instances of the class Person:
p1 = Person('TalksInfo', 2) p2 = Person('Author', 30)
In this article, we saw about what are classes and objects for Python and its importance. In our next article, we will see about how to use class and objects in real time scenario.
Check out other post related to Python