Static and Non Static Method in Python | Class Method in Python

 Static and Non Static Method in Python | Class Methods in Python






Methods:

  • Methods are used to define a business logic
  • There are three types of methods in python
A. Non-static Method

B. Class Method

C. Static Method


Non-static Method:-
  • Non-static methods are object level
  • Non-static method should have a first argument (Self)
    Note:- We can give any other argument name instead of self
Class Method:-
  • Class methods are class level
  • Use to have all the class level variable
  • It should have decorator @Class method
  • Must and should have one argument (cls)

Note:- We can give another value name instead of cls


Static Method:-
  • A static method is used to define small utility actions
Example:
  1. Addition of a number
  2. Subtraction of a number etc.
  • · It should have decorator @Static method

Example:



Question:- How to access the static method?


Question: Where we can call the static variable?

Answer: We can call under constructor, non-static method, class method, and static method as well.






Non-static method:-



A static variable can be called inside a constructor, non-static method, class method, and static method using the class name.


  • It is possible to change the static variable values inside a constructor
  • It is possible to change the static variable values inside a non-static method
  • Similarly, we can change the static variable value inside a non-static method, class method, and static method.



·  Non-static variables cannot access inside a class method or static method using self.


Question: - List the differences between constructor and method?


Constructor

Method

Identifier as __init__

Any valid identifier name

Never returns values

It will return a value (Single or multiple)

Automatically executed (When creating an object)

Should be called explicitly

Object-level

Object and class level


Question: - Write a python program to print how many objects were created for a class?














Comments

Popular Post

Root Cause Analysis in Testing | Easy way to explain in interview about RCA

Agile Testing Interview Questions and Answers | Easy way to explain the Agile Testing Interview Questions.

Data Types in Python with explanation

How to Run Python Code in IDLE

OOPS in python | Object Oriented Programming Languages and Systems | Static and Non Static in Python

Python IDEs and Code Editor | PyCharm

What is the difference between client-side validation and server-side validation?

Operators in Python and Types of Operators in Python

How to Download Python ?