0% found this document useful (0 votes)
5 views9 pages

Unit III File Handling, Classes - Part6

The document provides multiple examples of Python class definitions and their usage, demonstrating concepts such as instance variables, methods, and object manipulation. It includes examples of creating a 'student' class, using objects as arguments in functions, and returning objects from methods. The examples illustrate how to access class attributes and methods, as well as how to print information related to objects.

Uploaded by

Saraswathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Unit III File Handling, Classes - Part6

The document provides multiple examples of Python class definitions and their usage, demonstrating concepts such as instance variables, methods, and object manipulation. It includes examples of creating a 'student' class, using objects as arguments in functions, and returning objects from methods. The examples illustrate how to access class attributes and methods, as well as how to print information related to objects.

Uploaded by

Saraswathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

example:

 self: implict instance s1=student()


print("roll_no:",s1.roll_no)
#example for simple class print("Name:",s1.name)
class student: print("branch:",s1.branch)
roll_no=77 s1.read()
name="sukshma" -------
branch="cse" roll_no: 77
def read(self): Name: sukshma
print("reading...") branch: cse
reading...
example 2
#example for simple class s1=student()
class student: print("roll_no:",s1.roll_no)
roll_no=77 print("Name:",s1.name)
name="sukshma" print("branch:",s1.branch)
branch="cse" s1.read()
def read(self): ----
rollno=5 output:
print("rollno:",rollno) ???
print("reading...")
example3:
#example for simple class
class student:
roll_no=77
name="sukshma"
branch="cse"
def read(self):
rollno=5
print("rollno:",rollno)
print("instance variable",self.roll_no)
print("reading...")
example 3(conti..)
s1=student()
print("roll_no:",s1.roll_no)
print("Name:",s1.name)
print("branch:",s1.branch)
s1.read()
----------------
output:roll_no: 77
Name: sukshma
branch: cse
rollno: 5
instance variable 77
reading...
example 4:
#example for simple class
class student:
roll_no=77
name="sukshma"
branch="cse"
def read(self):
rollno=5
print("rollno:",rollno)
conti..
print("instance variable",self.roll_no)
print("reading...")
def write(self):
print("writing..")
s1=student()
print("roll_no:",s1.roll_no)
print("Name:",s1.name)
print("branch:",s1.branch)
s1.read()
s1.write()
Object as Arguments:
class Emp:
def __init__(self, name, designation):
self.name=name
self.designation=designation

def print_emp_info(emplist):
print(f’Emp Name is “{emplist.name}”)
print(f”Designation is “{emplist.designation}”)

e1 = Emp(“John”,”Manager”)
print_emp_info(e1)
class Track:
def __init__(self, song, art):
self.song=song
self.art=art
def print_track_info(vocalist):
print(f”Song is “{vocalist.song}”)
print(f”Artist is “{vocalist.art}”)
singer = Track(“Song name”,”Robert”)
print_track_info(singer)
Object as Return Values:
class Circle:
def __init__(self,radius):
self.radius=radius
self.status=""
def check_point_status(self):
if self.radius<30:
self.status="Green"
return self def main():
point = Circle(10)
obj=point.check_point_status()
print(obj.status)
print(isinstance(point,Circle))
print(isinstance(obj,Circle))
print(id(point))
print(id(obj))

if __name__ == "__main__":
main()

You might also like