Python Class

In Object Oriented Programming (OOP) Class is a core concept. It works like a template or blueprint or set of instructions and functionality to build a specific type of object. This tutorial will clarify all about class and objects.


Objectives

  • Basic understanding of Python class & objects

  • Python create class and object

  • A clear understanding of class methods

  • Python class properties and constructor


Basic understanding of Python class & objects

Class :

Out there you will find various definition of a class, but I am going to give you a practical definition of a Class. Class contains various methods or functions and there can or can’t be property, those are responsible for do specific task.

Object :

Class object is way to access class functionality for accomplish problem solutions


Python create class and object

Code

# Define a class
class Person:
    first_name: str = "Touhid"
    last_name: str = "Mia"

    def get_name(self):
        return self.first_name + " " + self.last_name


# Create Object & Access function
# Object creation
person = Person()

# Access object function
name = person.get_name()
print(name)

Output

Touhid Mia

Process finished with exit code 0


A clear understanding of Python class methods

Class method example

# Define a class
class Person:
    first_name: str
    last_name: str

    # Constructor
    def __int__(self, first_name, last_name, email=None):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email  # Assign value to object property

    # Method without parameter and with return
    def get_name(self):
        return self.first_name + " " + self.last_name

    # Method with parameter
    def print_phone(self, phone):
        print("Phone number " + phone)

    # Method with parameter default value
    def print_age(self, age=0):
        print("My age is " + str(age))

    # Private method
    def _private_method(self):
        print("This is private method")
Here
  • self parameter : The self parameter is a reference to the current instance of the class, and is used to access variables and other methods that belongs to the class.

  • Constructor : It is always executed when the class is being initiated. And It can assign values to object properties

  • Without Parameter and with return : Method can define without parameter and can also return value

  • With parameter : During the method creation you may pass data by parameter

  • Parameter with default value : Optional parameter, if it sent by the caller then will show that, otherwise will show default one

  • Private method : Python has no concept of access modifier but if any method or property start with "_" underscore, then it consider as private.


Python class properties and constructor

Class property example

# Define a class
class Person:

    # Property
    first_name: str
    last_name: str

    # Constructor
    def __int__(self, first_name, last_name, email):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email  # Assign value to object property

In short property is the global variable of a class, those are accessible to all method. Above class there is 2 property declared but this is not the right way to declare property, I have declared because this help when you write code in IDE, it will give suggestion.

Using constructor, initiate property is the right way of property definition, above class email and self.email = email is the way of property definition


Python Class video tutorial