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
We can call instance method by using object reference
ReplyDeleteWe can call class method by using class name or object reference
we can call static method by using class name or object reference