Python static method | staticmethod() decorator – Example

Using staticmethod() or @staticmethod can static method in Python. The static methods cannot modify the state of an object as they are not bound to it. Python static method examples Simple example code. Using staticmethod() The @staticmethod is a built-in decorator that defines a static method in the class. Output: Using @staticmethod Using @staticmethod annotation Read More…


Python class static variable | Example code

Which variables declared inside the class definition, but not inside a method are class or static variables in Python. This is different from C++ and Java, but not so different from C#, where a static member can’t be accessed using a reference to an instance. When defining some member variable outside any member method, the Read More…


Python module global variable | Example code

To create a module global variable you just need to declare the variable as global in Python. Global variables are also accessible from outside the module by using module_name dot var_name. Syntax Python module global variable example Simple example code the only global variables Python really has are module-scoped variables. Let’s make a module-global variable Read More…



Python class __init__ method | Basics

Python class init method is a reserved method in python classes. It represents a constructor in Python. When an object creates it is called and it allows the class to initialize the attributes of a class. As you know if you know another object-oriented programming constructors are used to initialize the object’s state. It is Read More…


A local variable in Python | Example code

A variable declared inside the function is called a local variable in Python. Local variables are accessible only inside the function. Example local variable in Python Simple example code creates a local variable inside a function and tries to access it outside those functions. Output: shows an error (NameError) because we are trying to access Read More…


How to declare private variable in Python | Example code

Python does not have any private variables as Java does. Access member variable is available in Python. However, don’t need private variables in Python, because it’s not bad to expose your classes member variables. The double underscore “__” does not mean a “private variable”. Use it to define variables that are “class local” and which Read More…



Super keyword in Python? | Example

There is no super keyword instead Super function is used in Python to give access to methods and properties of a parent or sibling class. Syntax This function returns an object that represents the parent class. Example Super keyword function in Python Simple example codes the class that will inherit all the methods and properties Read More…


Python multiple constructors | Example code

In Python, you can’t define multiple constructors. However, you can define a default value if one is not passed. Way to Achieve Multiple Constructors in Python Multiple constructors come to use when a defined class has to perform different functions. You can do Python Constructor overloading based on arguments as Multiple Constructors. Do if-elif blocks Read More…