Thursday, February 6, 2020

Class in Python

Python Classes


Class


Python is an object-oriented programming language. Classes and objects are basic building blocks of OOP. We can create classes and objects in Python. A class is made of two basic things, the data, and the code. Classes provide us the mechanism to keep the data and the code to access that together.

The data is referred to as attributes and the code is referred to as methods or functions within the class. Objects are the instances of a class. We can create several objects of a class. Whenever the attributes inside a class are populated with values (state), a new object is manufactured. In other words, a class can be considered as a factory of objects, or a blueprint or a template for creating the same type of individual objects.


class and objects in python
Class Dog with 6 objects(each having different color attribute value)


Classes in Python 


We can list a few important things about classes as,

  • A class can be created using the class keyword in Python.
  • Members of the class can be accessed by using dot(.) operator.
  • A class conceptually has attributes or fields (the data) and behavior (methods/functions)
  • Attributes and methods can be declared as normal variables and functions in Python.
  • The __init__() method is called the initializer and equivalent to the constructor in other object-oriented programming languages like Java.
  • The initializer can be considered as a method that is used to create a new object or new instance of the class.
  • Attributes or fields are defined first in a class and are known as the class attributes.
  • Attributes that belong to a particular instance of a class are known as instance attributes. We can define them inside __init__(), however, this is not mandatory, but it is good practice (because attributes defined outside of __init__() are exposed to the risk of being retrieved before they are defined). Every method, included in the class definition passes the object in question as to its first parameter.
  • The “self” is used for this reference(something like “this” keyword in Java).
  • By default, all class members exhibit the behavior like public keyword of the C++/Java.(private is missing by default)
  • Few class methods may be like __function-name__(self, arg). These methods are known as "magic methods" and are an important part of classes in Python. For instance, to implement operator overloading in Python the magic methods can be used.
  • __str__() is a special method that is run when Python tries to cast the object to a string. This method returns a string when using print(), etc

Declaring Class and Instance Variables In Python


The variables with an assigned value in the class declaration are class variables in Python and the variables inside methods and constructors are instance variables.

self 


Class methods need to carry an extra first parameter in the method definition. If we have a method that takes no arguments, then we still have to have one argument. We do not need to specify a value for self parameter when we call the method, Python automatically provides it.

Creating a class in Python


#Creating simple class Dog
class Dog:
  color='no color'
  
  def __init__(self,color):
    self.color=color
    
  def displaydog(self):
    print('This dog\'s color is '+self.color)
    
#Creating the object
blackdog=Dog('Black') #object 1
blackdog.displaydog()

Whitedog=Dog('White') #Object 2
Whitedog.displaydog()

#We can create n number of objects from a single class
#Class is like factory or template to create multiple same kinds
# of objects.

Output:

This dog's color is Black
This dog's color is White

Creating objects of a class