Class, Object and Inheritance in Python 04.06.2019
Class, Object and Inheritance in Python 04.06.2019
SANTOSH VERMA
Important Note
Please
remember that Python completely
works on indentation.
Classes
Just like every other Object Oriented Programming
language Python supports classes. Let’s look at some
points on Python classes. Classes are created by
keyword class.
# A sample method
def fun(self):
print("Hello")
# Driver code
obj = Test()
obj.fun()
Output: Hello
The self
Class methods must have an extra first parameter in method
definition. We do not give a value for this parameter when
we call the method, Python provides it.
def Main():
# Creating an object of the MyClass.Here, 'me' is the
object
me = MyClass()
class Person:
# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)
p = Person(‘Santosh')
p.say_hi()
Methods
def Main():
# vec is an object of class Vector2D
vec = Vector2D() Output: X: 5, Y:
6
# Passing values to the function Set
# by using dot(.) operator.
vec.Set(5, 6)
print("X: " + vec.x + ", Y: " + vec.y)
if __name__=='__main__':
Main()
Python In-built class functions
Inheritance
class derived-classname(superclass-name)
class Person(object): # class Person: of Python 3.x is same as defined here.
# Driver code
emp = Person(“Ram") # An Object of Person
print(emp.getName(), emp.isEmployee())
Thank You!