Python Classes


A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by their class) for modifying their state.

To understand the need for creating a class let’s consider an example, let’s say you wanted to track the number of laptops that may have different attributes like brand, RAM, Storage, etc... If a list is used, the first element could be the laptop's brand while the second element could represent its RAM & the third element represents its Storage. Let’s suppose there are 100 different laptops, then how would you know which element is supposed to be which? What if you wanted to add other properties to these laptops? This lacks organization and it’s the exact need for classes. 

The class creates a user-defined data structure, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

Some points on Python class:  

  • Classes are created by the keyword: class.
  • Attributes are the variables that belong to a class.
  • Attributes are always public and can be accessed using the dot (.) operator. Eg.: ClassName.Attributename

An example:



Class Objects


An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. It’s not an idea anymore, it’s an actual laptop, like a laptop of brand MI, with 8GB RAM, 256GB Storage. You can have many laptops to create many different instances, but without the class as a guide, you would be lost, not knowing what information is required.
An object consists of : 

  • State: It is represented by the attributes of an object. It also reflects the properties of an object.
  • Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.


Declaring Objects (Also called instantiating a class)


When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.


Declaring an object Example:



The self

  • Class methods must have an extra first parameter in the method definition. We do not give a value for this parameter when we call the method, Python provides it.
  • If we have a method that takes no arguments, then we still have to have one argument.
  • This is similar to this pointer in C++ and this reference in Java.

When we call a method of this object as myobject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.

__init__ method

The __init__ method is similar to constructors in C++ and Java. Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation. It runs as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.



Output:


Brand: MI, RAM: 8GB, Storage: 256GB

Brand: Lenovo, RAM: 16GB, Storage: 256GB








No comments:

Post a Comment