Python OOP Part 1
Python is an object-oriented programming language. Object-oriented
programming (OOP) focuses on creating reusable patterns of code.
- Class — A blueprint created by a programmer for an
object. This defines a set of attributes that will characterize any object
that is instantiated from this class.
- Object — An instance of a class. This is the realized
version of the class, where the class is manifested in the program.
We define classes by using the
class
keyword, similar to how we define functions by using
the def
keyword.
Objects can store data using ordinary
variables that belong to the object. Variables that belong
to an object or class are referred to as fields. Objects
can also have functionality by using functions that belong to a
class. Such functions are called methods of the
class. This terminology is important because it helps us to differentiate
between functions and variables which are independent and those which belong to
a class or object. Collectively, the fields and methods can be referred to as
the attributes of that class.
Fields are of two types - they can belong
to each instance/object of the class or they can belong to the class itself.
They are called instance variables and class
variables respectively.
A class is created
using the
class
keyword. The fields and methods of
the class are listed in an indented block.
The self
Class methods have only one specific
difference from ordinary functions - they must have an extra first name that
has to be added to the beginning of the parameter list, but you do not give
a value for this parameter when you call the method, Python will provide it.
This particular variable refers to the object itself, and by
convention, it is given the name self.
Although, you can give any name for this
parameter, it is strongly recommended that you use the
name self - any other name is definitely
frowned upon. There are many advantages to using a standard name - any reader
of your program will immediately recognize it and even specialized IDEs
(Integrated Development Environments) can help you if you use self.
class Student:
pass # An empty block
s = Student ()
print(s)
We create a new class using the
class
statement and the name of the
class. This is followed by an indented block of statements which form the body
of the class. We create an object/instance of this class using the name of the
class followed by a pair of parentheses.
class Student:
def say_hi(self):
print('Hello,
how are you?')
s = Student ()
print(s)
The __init__
method
The
__init__
method is run
as soon as an object of a class is instantiated (i.e. created). The method is
useful to do any initialization (i.e. passing initial values
to your object) you want to do with your object. Notice the double underscores
both at the beginning and at the end of the name.
Class and Instance Variables (Or attributes)
In
Python, instance variables are variables whose value is assigned inside a
constructor or method with self.
Class
variables are variables whose value is assigned in class.
# Class for BCA Student
class BCAStudent:
# Class Variable
stream = 'bca'
# The init method
or constructor
def __init__(self,
roll):
# Instance
Variable
self.roll
= roll
# Objects of BCAStudent class
a = BCAStudent(101)
b = BCAStudent(102)
print(a.stream)
print(b.stream)
print(a.roll)
print(b.roll)
# Class variables can be accessed using class name also
print(BCAStudent.stream)
Output:
bca
bca
101
102
bca
Comments
Post a Comment