[go: up one dir, main page]

0% found this document useful (0 votes)
8 views7 pages

OOP in Python

OOP in Python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

OOP in Python

OOP in Python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python for Beginners

Requests:
1. Defining a Class: 3. Instance Methods:
A class is defined using the class keyword, followed by the Methods are functions defined within a class that operate on the
class name (conventionally capitalized) and a colon. object's data. They also take self as their first parameter.
class MyClass: class Person:
x=5 def __init__(self, name, age):
self.name = name
p1 = MyClass() self.age = age
print(p1.x)
def myfunc(self):
print("Hello my name is " + self.name)
2. The __init__ Method (Constructor):
The __init__ method is a special method, often referred 4. Creating Objects (Instantiating the Class):
to as the constructor, which is automatically called when a To create an object from a class, you call the class name as if it were
new object (instance) of the class is created. It's used to a function, passing any required arguments for the __init__ method
initialize the object's attributes. The first parameter, self,
refers to the instance itself. p1 = Person("John", 36)
Create a class named Person, use the __init__() method to
assign values for name and age: 5. Accessing Attributes and Calling Methods:
class Person: You can access an object's attributes and call its methods using the
def __init__(self, name, dot (.) operator. print(p1.name)
age): . print(p1.age)
self.name = name
Requests:
Class: A Colleton of functions and attributes, attached to a specific
name, which represents an abstract concept.
Attribute: A named piece of data (i.e. variable associated with a class.
Object: A single concrete instance generated from a class
Instances of Classes: Classes can be viewed as factories or templates
for genera ng new object instances. Each object instance takes on the
proper es of the class from which it was created.
class Person:
def __init__(self, name, age):
self.name = name # Initialize the 'name' attribute
self.age = age # Initialize the 'age' attribute

def display_info(self):
print(f"Name: {self.name}, Age: {self.age}")

# Creating an instance of the Person class


person1 = Person("Alice", 30)

# Accessing and using the object's attributes and methods


person1.display_info() Create a class (blueprint)
Create a instance (Object)
Class vs instance
Instance attributes: defined in __init__(self)
Class attribute
Matplotlib:
Inheritance Polymorphism
Inheritance allows us to define a class that
The word "polymorphism" means "many forms", and in
inherits all the methods and properties from
programming it refers to methods/functions/operators with the
another class.
same name that can be executed on many objects or classes.
Parent class is the class being inherited from,
Two objects of different classes but supporting the same set of
also called base class.
functions or attributes can be treated identically. The
Child class is the class that inherits from another
implementations may differ internally, but the outward
class, also called derived class.
Class inheritance is designed to model relationships of the type "appearance" is the same.
"x is a y" (e.g. "a triangle is a shape")
Matplot
Encapsulation
OOP – Object Oriented Programming
• Instance method
• Dunder method
In Python's Object-Oriented Programming (OOP), dunder methods (also known as "magic methods" or "special methods") play
a crucial role in how inheritance functions and how objects behave. These methods are identified by their double underscores
at the beginning and end of their names (e.g., __init__, __str__, __add__).
• Static method

You might also like