Tuesday 8 March 2022

Python#07

Class in Python

A Class in Python is a logical grouping of data and functions. It gives the freedom to create data structures that contains arbitrary content and hence easily accessible.

Class” is a logical grouping of functions and data. Python class provides all the standard features of Object Oriented Programming.

Class inheritance mechanism :  A derived class that override any method of its base class. A method can call the method of a base class with the same name. Python Classes are defined by keyword “class” itself. Inside classes, you can define functions or methods that are part of the class

Everything in a class is indented, just like the code in the function, loop, if statement, etc.

The self argument in Python refers to the object itself. Self is the name preferred by convention by Pythons to indicate the first parameter of instance methods in Python

Python runtime will pass “self” value automatically when you call an instance method on in instance, whether you provide it deliberately or not

In Python, a class can inherit attributes and behavior methods from another class called subclass

class ametClass():
def aMethod(self):
print("Men Money Machine & Matters")

def bMethod(self, param):
print("Hey I am testing class method :" + param)

def main():
c = ametClass()
c.aMethod()
c.bMethod('I am param porul')

main()

Result:

Men Money Machine & Matters

Hey I am testing class method :I am param porul

####inheritance

class aClass():

def aMethod(self):
print('A ha : Base class method 1')

def bMethod(self, str):
print('oh oh Base class method 2 '+str)

class childClass(aClass):

def cMethod(self):
aClass.aMethod(self) # Note instance of base class method
print('A ha : Child class Method1.....')

def dMethod(self, str):
print('Oh Oh Child class method 2'+str)

def main():

c1 = aClass()
c1.aMethod() # Note child inherited from aClass
c1.bMethod('Base class ..nna') # Note child inherited from aClass

c2 = childClass()
c2.aMethod()
c2.bMethod('Besh besh Child Class Method1')
c2.cMethod()
c2.dMethod('Nanna Irukku Child Class Method 2')

main()

Result

A ha : Base class method 1

oh oh Base class method 2 Base class ..nna

A ha : Base class method 1

oh oh Base class method 2 Besh besh  Child Class Method1

A ha : Base class method 1

A ha : Child class Method1.....

Oh Oh Child class method 2Nanna Irukku Child Class Method 2

Using Constructors
# Constructors : Defining and initalizing  values

class Cadet:

name = " " # Class Variables
def __init__(self, name):
self.name = name

def printWelcome(self):
print('Welcome to Kalvi Koil, AMET '+ self.name)

Cadet1 = Cadet('Anbu')

Cadet1.printWelcome()

Result :

Welcome to Kalvi Koil, AMET  Anbu

The above sample codes explain the how to define a class, define class method, inherit a class, overloading a method, initialize the class using constructor.

Happy Learning by simple super code!

No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...