# 02-Python Cont. ###### tags: `GENERAL` # Class and Object A class is a blueprint for the object. ``` class Parrot: pass ``` An object (instance) is an instantiation of a class. When class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated. ``` obj = Parrot() ``` Here, obj is an object of class Parrot. ``` class Parrot: # class attribute species = "bird" # instance attribute def __init__(self, name, age): self.name = name self.age = age # instantiate the Parrot class blu = Parrot("Blu", 10) woo = Parrot("Woo", 15) # access the class attributes print("Blu is a {}".format(blu.__class__.species)) print("Woo is also a {}".format(woo.__class__.species)) # access the instance attributes print("{} is {} years old".format( blu.name, blu.age)) print("{} is {} years old".format( woo.name, woo.age)) ``` Output ``` Blu is a bird Woo is also a bird Blu is 10 years old Woo is 15 years old ``` ## Methods Methods are functions defined inside the body of a class. They are used to define the behaviors of an object. ``` class Parrot: # instance attributes def __init__(self, name, age): self.name = name self.age = age # instance method def sing(self, song): return "{} sings {}".format(self.name, song) def dance(self): return "{} is now dancing".format(self.name) # instantiate the object blu = Parrot("Blu", 10) # call our instance methods print(blu.sing("'Happy'")) print(blu.dance()) ``` Output ``` Blu sings 'Happy' Blu is now dancing ``` ## Inheritance Inheritance is a way of creating a new class for using details of an existing class without modifying it. The newly formed class is a derived class (or child class). Similarly, the existing class is a base class (or parent class). ``` # parent class class Bird: def __init__(self): print("Bird is ready") def whois_this(self): print("Bird") def swim(self): print("Swim faster") # child class class Penguin(Bird): def __init__(self): # call super() function super().__init__() print("Penguin is ready") def whois_this(self): print("Penguin") def run(self): print("Run faster") peggy = Penguin() peggy.whois_this() peggy.swim() peggy.run() ``` Output ``` Bird is ready Penguin is ready Penguin Swim faster Run faster ``` # File operations and context managemnet ## File operations Opening Files in Python Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. ``` f = open("test.txt") # open file in current directory f = open("C:/Python38/README.txt") # specifying full path ``` ``` f = open("test.txt") # equivalent to 'r' or 'rt' f = open("test.txt",'w') # write in text mode f = open("img.bmp",'r+b') # read and write in binary mode ``` ``` f = open("test.txt", encoding = 'utf-8') # perform file operations f.close() ``` ``` try: f = open("test.txt", encoding = 'utf-8') # perform file operations except Error as e: print('error occured') finally: f.close() ``` ## context management ``` with open("test.txt", encoding = 'utf-8') as f: # perform file operations ``` ``` with open("test.txt",'w',encoding = 'utf-8') as f: f.write("my first file\n") f.write("This file\n\n") f.write("contains three lines\n") ``` # Modules # Task