Skip to main content

Python OOPs Concepts: Using Variables and Methods

 

Types of Variables in OOPs Python

 

Instance Variable

Static Variable

Local Variable

 

Object Level Variables

Class Level Variables

Method Level Variables

When to use:

For Every Object if you want Separate copy, use Instance Variables

For all object one copy is required, use static variables

Inside method, Just used for temporary requirement

Where to Declare

Inside the constructor method (in general)

Within the class directly, outside of methods (in general)

 

Within the method only.

How to Declare

Within the constructor: Instance variables can be declared within the constructor method using the self.

 

Using default values: Instance variables can be assigned default values during initialization.

 

Outside the class: use object name.

 

·         Within the class directly

·         Inside the classmethod: cls or classname

·         Remaining Places: By using classname

Only inside the method

How to access

Using self, Object Reference. (Inside the class, use self, outside the class use object name)

Using classname, cls, self,  Object reference

Only inside the method

How to Modify or update

can be accessed and modified from any other method within the class using the self keyword.

Outside the class: use object name

 

Can be modified by using classname or cls

Can not be accessed / modified from outside of the method

How to Delete

Use self or object name

Use classname or cls

Delete inside the method directly using del

 

Types of methods

 

Instance Methods

Class Methods

Static Methods

 

Object Related Method

Class Related Method

General Utility Method

 

this method is always talks about a particular object.

Method always talks about class level and common to all instances of the class

No way related to any object or class.

Variables used in the method

Using at least one instance variable (whether using static variables or not)

 

not using any instance variable, but using static variables and or local variables

Not using any instance variables and static variables. Using only local variables

Use Instance Variable (must) + static variable (Optional) + local variable (Optional)

Use Static variable + local variable

Use only local variable

First Argument of the method

First argument to the instance method is always self, which is the reference variable to the current object.

First argument to the class method is always cls, which is the reference variable to class level object

No to self

No to cls

Decorators used

No decorators are used

@classmethod decorator is used

@staticmethod decorator is used

 

Access Specifiers

In Python, access specifiers are used to control the visibility and accessibility of class attributes and methods. There are three main access specifiers in Python: public, protected, and private.

 

public:

·         Attributes and methods marked as public can be accessed from anywhere, both within the class and outside of it.

·         By default, all members in a Python class are public.

·         Members declared as public have a NO underscore prefix in their names. (ex: area, square())

protected:

·         Attributes and methods marked as protected are intended to be used only within the class and its subclasses.

·         Members declared as protected have a single underscore prefix in their names( ex: _method())

·         These members are not directly accessible from outside the class, but they can still be accessed within derived classes.

·         The use of protected members is generally discouraged in Python

private:

·         Attributes and methods marked as private are intended to be used only within the class.

·         In Python, private access is indicated by two underscores before the attribute or method name (e.g. __attribute or __method()).

·         These members are not directly accessible from outside the class, but they can still be accessed using the _classname__membername syntax. This is known as name mangling


Comments

  1. We can call instance method by using object reference
    We can call class method by using class name or object reference
    we can call static method by using class name or object reference

    ReplyDelete

Post a Comment

Popular posts from this blog

Inheritance

Inheritance is a fundamental concept in object-oriented programming, which allows a class to inherit properties and methods from another class. There are several types of inheritance, including: Single Inheritance: In single inheritance, a subclass inherits properties and methods from a single parent class. The subclass is said to be derived from the parent class. Multiple Inheritance: Multiple inheritance allows a subclass to inherit properties and methods from multiple parent classes. In this case, the subclass is said to have multiple base classes. However, multiple inheritance can lead to complexity and ambiguity in the code. Multilevel Inheritance: Multilevel inheritance occurs when a subclass inherits properties and methods from a parent class, which in turn inherits from another parent class. In this case, the subclass is said to be derived from both the parent class and the grandparent class. Hierarchical Inheritance: Hierarchical inheritance occurs when multiple subclasses

Polymorphism: Method Overloading vs Method Overriding

  Method Overloading In object-oriented programming languages, method overloading enables a class to have several methods with the same name but different parameters. However, in Python, method overloading is not directly supported as opposed to languages such as Java or C++. This is because Python allows developers to define default arguments for their methods and pass arguments of any type to a method. This flexibility allows a single method to handle various types of arguments, eliminating the need for overloading.   However, there is a way to simulate method overloading in Python by using default argument values or variable length arguments and conditional statements. Here's an example: Program using default arguments:       Program using variable length arguments:   Multiple methods with Same Name: When we define multiple methods with same name, Python will consider the last defined method only. Python will not support method overloading. ( Why? Method overlo