Classes and Objects in Python

 

Classes and Objects in Python

Class:

A class is a template or blueprint for creating objects in Python.

It defines the properties (attributes) and behaviors (methods) that objects of the class will have.

It serves as a blueprint from which multiple objects can be created.

Object:

An object is an instance of a class.

It represents a specific entity or instance based on the class blueprint.

Each object has its own set of attributes and can perform actions (methods) defined in the class.

Classes and objects are fundamental concepts in object-oriented programming (OOP), allowing for code organization, modularity, and reusability. They are widely used in Python for building complex systems and applications.

In Python, a class is a blueprint for creating objects (instances). It defines the attributes (data) and methods (functions) that all objects of that class will have.

 

An object is an instance of a class. It is a concrete realization of the class blueprint, with its own unique data and behavior.

 

An instance refers to a specific occurrence or realization of an object, created from a class. Every object is an instance of some class.

 

Here's a simple analogy:

 

Class: Blueprint for a house. It defines the structure, layout, and features that all houses of that type will have.

 

Object: A specific house built according to the blueprint. Each house has its own unique characteristics, such as color, size, and interior design.

 

Instance: A particular house, like "MyHouse", which is an instance of the House class. It's a specific realization of the blueprint, with its own unique attributes.

 

In Python, you define a class using the class keyword, and you create objects (instances) of that class by calling the class like a function. For example:

 

How to Define Class in Python:

1.     Inside the Class:

Syntax,

class MyClass:

    def method_inside(self):

        # Method code here

# Creating an instance of the class

obj = MyClass()

# Calling the method

obj.method_inside()

In this syntax, method_inside is defined within the class MyClass. When an instance of MyClass is created (obj), the method can be called using dot notation (obj.method_inside()).

Example,

class InterestCalculator:

    def calculate_simple_interest( self,principal, rate, time):

        interest = (principal * rate * time) / 100

        return interest

   

    def display_result( self,principal, rate, time):

        interest = self.calculate_simple_interest(principal, rate, time)

        print("Principal Amount:", principal)

        print("Rate of Interest:", rate)

        print("Time for loan:", time)

        print("Simple Interest:", interest)

 

# Creating an instance of the class

calculator = InterestCalculator()

 

# Calling the method to calculate and display the result

calculator.display_result(1000, 5, 2)

Output:

Principal Amount: 1000

Rate of Interest: 5

Time for loan: 2

Simple Interest: 100.0

In this example, calculate_simple_interest is defined within the InterestCalculator class. It takes principal, rate, and time as parameters and calculates the simple interest. An instance of InterestCalculator is created, and the method is called using dot notation (calculator.calculate_simple_interest(...)).

 

Inside Class Method Using User Input

Example,

class InterestCalculator:

    def calculate_simple_interest(self, principal, rate, time):

        interest = (principal * rate * time) / 100

        return interest

   

    def display_result(self, principal, rate, time):

        interest = self.calculate_simple_interest(principal, rate, time)

        print("Principal Amount:", principal)

        print("Rate of Interest:", rate)

        print("Time for loan:", time)

        print("Simple Interest:", interest)

 

# Creating an instance of the class

calculator = InterestCalculator()

 

# Taking user input for principal amount, rate of interest, and time

principal = float(input("Enter Principal Amount: "))

rate = float(input("Enter Rate of Interest: "))

time = float(input("Enter Time for Loan (in years): "))

 

# Calling the method to calculate and display the result

calculator.display_result(principal, rate, time)

Output:

Enter Principal Amount: 5000000

Enter Rate of Interest: 8.50

Enter Time for Loan (in years): 20

Principal Amount: 5000000.0

Rate of Interest: 8.5

Time for loan: 20.0

Simple Interest: 8500000.0

 

2.       Outside the Class:

class MyClass:

    pass

def method_outside(self):

    # Method code here

# Attaching the method to the class

MyClass.method_outside = method_outside

# Creating an instance of the class

obj = MyClass()

 

# Calling the method

obj.method_outside()

In this example, method_outside is defined outside the class MyClass. It's then attached to the class using dot notation (MyClass.method_outside = method_outside). After attaching, instances of MyClass can call this method.

Methods defined inside a class automatically receive a reference to the instance as the first parameter (self), which allows them to access instance attributes and other methods.

Methods defined outside the class need to explicitly accept an instance as the first parameter if they want to operate on instance data. This parameter is usually named self, but you can use any name.

In Python, self is a reference to the current instance of a class. When defining methods within a class, the first parameter of each method is always self, which represents the instance of the class.

Here's what self does in Python:

Accessing Instance Variables: With self, you can access instance variables (attributes) within the class. It allows you to differentiate between instance variables and local variables within methods.


Calling Other Methods: self is used to call other methods within the same class. It ensures that the correct method is called on the current instance.


Creating Instance-specific Attributes: Using self, you can create instance-specific attributes. These attributes are unique to each instance of the class.

Example,

# Define the InterestCalculator class

class InterestCalculator:

    pass

 

# Define the calculate_simple_interest function outside the class

def calculate_simple_interest(self, principal, rate, time):

    print("Principal Amount:", principal)

    print("Rate of Interest:", rate)

    print("Time for loan:", time)

   

    interest = (principal * rate * time) / 100

    return interest

 

# Define the display_result function outside the class

def display_result(principal, rate, time, interest):

    print("Principal Amount:", principal)

    print("Rate of Interest:", rate)

    print("Time for loan:", time)

    print("Simple Interest:", interest)

 

# Attach the calculate_simple_interest function to the InterestCalculator class

InterestCalculator.calculate_simple_interest = calculate_simple_interest

 

# Create an instance of the InterestCalculator class

calculator = InterestCalculator()

 

# Call the calculate_simple_interest method on the instance

interest = calculator.calculate_simple_interest(1000, 5, 2)

 

print("Simple Interest is:",interest)

Output:

Principal Amount: 1000

Rate of Interest: 5

Time for loan: 2

Simple Interest is: 100.0

In this example, calculate_simple_interest is defined outside the InterestCalculator class. It's then attached to the class using dot notation (InterestCalculator.calculate_simple_interest = calculate_simple_interest). After attaching, instances of InterestCalculator can call this method.

Outside Class Method Using User Input:

# Define the InterestCalculator class

class InterestCalculator:

    pass

 

# Define the calculate_simple_interest function outside the class

def calculate_simple_interest(self, principal, rate, time):

    print("Principal Amount:", principal)

    print("Rate of Interest:", rate)

    print("Time for loan:", time)

   

    interest = (principal * rate * time) / 100

    return interest

 

# Define the display_result function outside the class

def display_result(principal, rate, time, interest):

    print("Principal Amount:", principal)

    print("Rate of Interest:", rate)

    print("Time for loan:", time)

    print("Simple Interest:", interest)

 

# Attach the calculate_simple_interest function to the InterestCalculator class

InterestCalculator.calculate_simple_interest = calculate_simple_interest

 

# Create an instance of the InterestCalculator class

calculator = InterestCalculator()

 

# Take user input for principal amount, rate of interest, and time

principal = float(input("Enter Principal Amount: "))

rate = float(input("Enter Rate of Interest: "))

time = float(input("Enter Time for Loan (in years): "))

print("----------------------------------------------------")

# Call the calculate_simple_interest method on the instance

interest = calculator.calculate_simple_interest(principal, rate, time)

 

# Display the result

print("Simple Interest is:", interest)

Output:

Enter Principal Amount: 10000

Enter Rate of Interest: 10

Enter Time for Loan (in years): 16

----------------------------------------------------

Principal Amount: 10000.0

Rate of Interest: 10.0

Time for loan: 16.0

Simple Interest is: 16000.0

टिप्पणी पोस्ट करा

0 टिप्पण्या