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

ORACLE Express Edition: Getting Started

1. Introduction to Oracle Database 21c Express Edition (XE) - Free, lightweight version of Oracle Database - Ideal for learning and small-scale applications - Limited to 12GB of user data and uses up to 2GB of RAM 2. Installation and Setup 2.1 Installing Oracle 21c XE 1. Download Oracle 21c XE from: https://www.oracle.com/database/technologies/xe-downloads.html 2. Run the installer:    - Windows: Double-click the .exe file    - Linux: Use `rpm` or `yum` command 3. Follow the installation wizard:  Accept the license agreement Choose an installation location (default is usually fine) Set a password for the SYS, SYSTEM, and PDBADMIN accounts (write this down!) Select the option to start the database service automatically (recommended)  4. Complete the installation: Wait for the installation process to finish Note down the database connection details provided at the end The default container database (CDB) name is XE The default pluggable database (PDB) nam...

Practical 1: Getting Started with MYSQL

 Getting Started with MySQL Introduction to MySQL Definition: MySQL is an open-source relational database management system (RDBMS) Uses: Web applications, data warehousing, e-commerce, logging applications Key features: Speed, reliability, scalability, and ease of use Installing MySQL Download MySQL Community Server from official website Follow installation wizard for your operating system Set root password during installation Verify installation: mysql --version MySQL Command-line Client Accessing MySQL: mysql -u root -p Basic commands: SHOW DATABASES ; CREATE DATABASE mydb ; USE mydb ; SHOW TABLES ; MySQL Workbench Introduction: Visual tool for database design and management Key features: SQL development Data modeling Server administration Example: Creating a new connection New Connection > Enter details (hostname, username, password) PHPMyAdmin Web-based MySQL administration tool Often comes pre-installed with web hosting packag...