Python – Class

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.

Syntax

# create class
class class_name():
   variables
   def __init__(self, parameters):
   functions

# create object of class
variable = class_name();

__init__() function

This special function gets called whenever a new object of that class is instantiated. This type of function is also called constructors in Object Oriented Programming (OOP). We normally use it to initialize some if not all variables within the class or perform any operations that are necessary for the object.

Object method

instance method or object method is a method that can freely access attributes and other methods on the same object. This gives them a lot of power when it comes to modifying an object’s state.Not only can they modify object state, instance methods can also access the class itself through the self.__class__ attribute. This means instance methods can also modify class state.

class User():
    name = str
    age = int

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def get_name(self):
        return self.name

class method

Because the class method only has access to this cls argument, it can’t modify object instance state. That would require access to self. However, class methods can still modify class state that applies across all instances of the class.

static method

This type of method takes neither a self nor a cls parameter (but of course it’s free to accept an arbitrary number of other parameters).

Therefore a static method can neither modify object state nor class state. Static methods are restricted in what data they can access – and they’re primarily a way to namespace your methods.

class User():
    name = str
    age = int
    admin_type = "ADMIN"
    user_type = "USER"

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def get_name(self):
        return self.name

    @classmethod
    def get_admin_role(cls):
        return "ROLE_"+cls.admin_type

    @staticmethod
    def get_user_role():
        return "ROLE_" + User.user_type
# class method
print(User.get_admin_role())

# static method
print(User.get_user_role())

 

You can delete properties on objects by using the del keyword

kinga = User("Kinga", 8)
print(kinga.get_name())
del kinga.name
print(f'name: {kinga.get_name()}')

# output
Kinga
name: <class 'str'>

class definitions cannot be empty, but if you for some reason have a class definition with no content, put in the pass statement to avoid getting an error.

class User():
    pass

 

Source code on Github

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.