0% found this document useful (0 votes)
2 views

Python Lecture 8

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Lecture 8

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PYTHON

LETS LEARN SOMETHING NEW


LECTURE 08

OOP (OBJECRT ORIENTED


PROGRAMMING)
Part 1
OOP IN PYTHON

To map with real world scenarios, we started using objects in code.


This is called object oriented programming.
CLASS AND OBJECT

Class is a blueprint for creating objects.


#creating class
class Student:
name = “karan kumar”
#creating object (instance of a class)
s1 = Student( )
print( s1.name )
_ _INIT_ _ FUNCTION

Constructor
All classes have a function called __init__(), which is always executed
when the object is being initiated.
#creating class
class Student:
def __init__( self, fullname ):
self.name = fullname
CLASS & INSTANCE ATTRIBUTES

Class.attr # for common attributes


obj.attr #for different attributes

College_name= class attr


Name,marks =object attr
METHODS

Methods are functions that #creating a method under the


belong to objects. class
#creating a class def hello( self ):
class Student: print( “hello”, self.name)
def __init__( self, fullname ):
self.name = fullname
LET‘S PRACTICE

Create student class that takes name & marks of 3 subjects as


arguments in constructor. Then create a method to print the average.
STATIC METHOD

Methods that don’t use the self parameter (work at class level)
class Student:
@staticmethod
def college( ):
print( “ABC College” )

*Decorators allow us to wrap another function in order to


extend the behavior of the wrapped function, without
permanently modifying it
IMPORTANT

Abstraction
Hiding the implementation details of a class and only showing the
essential features to the user.

Encapsulation
Wrapping data and functions into a single unit (object).
LETS PRACTICE

Create Account class with 2 attributes - balance & account no. Create
methods for debit, credit & printing the balance.
All power is within you.
You can do anything and everything.

You might also like