Never knew file handling and inheritance could be this easy…
Handling files just got easier with python. There are three basic modes , the read(‘r’) , write(‘w’) and append(‘a’).
The open() function is used to open a file: f=open(‘abc.txt’) . This open function needs two parameters: filename and mode. By default, i.e. when only filename is passed, the open function opens the file in read mode.
To close the file, f.close() is used.
There are several read methods, which are read(), readline() and readlines() each with different functions.
In write mode, the write() fucntion erases everything that was there before adding new texts to file but, in append mode, it adds texts at the end of the file without removing anything.
The seek() and tell() methods helps a lot in moving around the file. The seek() method takes you to your desired position and tell() method tells you your current position.
The concept of classes is same as that in java. The constructor is defined as def __init__(<parameters>). Instead of the ‘this’ keyword, here we use the ‘self’ keyword.
Objects are created by writing obj=Myclass() (where Myclass is the name of the class)
Inheritance was cooler, to inherit a superclass, the subclass just needs to have the superclass name in parenthesis in the class declaration statement. For example : Suppose the superclass name is person, then its subclass will be declared as:
‘ class student(person): ‘
For multiple inheritance, simply write the superclass names seperated by a comma: class myclass(superclass1, superclass2,….):
To delete an object, use ‘ del obj ‘, where obj is the object name.