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

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

Is Li-Fi Better than Wi-Fi?

Li-Fi  ( light fidelity )  is a bidirectional wireless system that transmit data to the devices like mobiles, laptop, etc., via infrared light or LED. The device has a receiver to pick up light signals and a transmitter to send light signal back to the lamp using infrared light or LED. It was first unveiled in 2011 and, unlike Wi-Fi, which uses radio frequency, Li-Fi technology only needs a light source with a chip to transmit an internet signal through light waves. Light fidelity (LiFi) is a faster, more secure and efficient wireless connection that uses light waves to transmit data Li-Fi technology still has a long way to go before worldwide adoption but every year, we are getting nearer to enjoying it for ourselves. The future surely looks bright with LiFi. How LiFi Works? LiFi makes use of visible light through overhead lighting for the transmission of data. This is possible through the use of a Visible Light Communications (VLC) system for data transmission. A VLC system has two q