Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

PythonClasses-Unit1

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

PythonClasses-Unit1

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

Unit-I

Python Classes
E.Annal Sheeba Rani, AP/IT
Classes
• Creates a user defined data structure
• Holds its own
- member data
- member functions
• They are accessed and used by creating an instance of that class
Classes
• Are created by a keyword class
• Attributes
- are the variables belong to that class
- they are always public
- they can be accessed using the dot operator
Class definition syntax
class classname:
Constructor
Member fun 1
member fun 2
.
.
Member fun n
Object Creation
Accessing member function
Class Objects
• An object is an instance of a class
• An object consist of
• State: It is represented by the attributes of the object
• Behaviour: It is represented by the methods of the object
• Identity: It gives an unique name to the object
- enables one object to interact with other objects
Example:
Class Student
name=“kumar” #attribute1
dept=“IT” #attribute2
def display(self): #method
print(“Student name:”,self.name)
print(“Department:”,self.dept)
S1=Student() #object creation
Print(S1.name)
S1.display()
output
kumar
Student name: kumar
Department: IT
Self Keyword
• Class methods must have an extra parameter in the method definition
• We do not give a value for this parameter
• Self represents the instance of the class
• By using the “self” keyword we can access the attributes and methods
of the class
Init()method
• Constructors are used to initialize the object’s state
• The init() method is used to create constructors in python
• Containers – contains a collection of statements
- executed at the time of object creation
• It accepts the self keyword as a first argument
- it allows accessing the attributes or methods of the class
Constructors can be of two types
1.Parameterized constructors
2.Non – parameterized constructors
1.Parameterized constructors

• We can pass any number of arguments at the time of creating the


class object
• Depending upon the init() definition
Example
class employee:
def_ _ init_ _(self,ename,edept):
self.ename=ename
self.edept=edept
def display(self):
print(“Emp name:”,self.ename)
print(“Emp dept:”,self.edept)
emp1=employee(“Arun”,”HR”)
emp2=employee(“Meena”,”Production”)
emp1.display()
emp2.display()

Output:
Emp name: Arun
Emp dept: HR
Emp name: Meena
Emp dept: Production
Non Parameterized constructors
• The non parameterized constructors are used when we don’t want to
manipulate the value of the constructor
• That has only self as argument
class employee:
def_ _init_ _(self):
print(“non parameterized constructor”)
def display(self,dept):
print(“Emp dept:”,self.dept)
emp=employee()
emp.display(“Testing”)

Output:
non parameterized constructor
Emp dept: Testing

You might also like