27
loading...
This website collects cookies to deliver better user experience
class Complex:
#Default values
x=0
y=0
def __init__(self,x,y):
self.x=x
self.y=y
def absolute(self):
return (self.x**2+self.y**2)**0.5
a=Complex(2,-3)
print(a.absolute())
b=Complex(4,5)
print(b.absolute())
3.605551275463989
6.4031242374328485
Why OOP?
In software development lifecycle, coding is fifth or sixth stage. First analysis of requirements of clients is done. Developers approach client and get technical review of the requirement, discuss at length with client. Unless we get clarity as what and how is expected coding doesnt start. Then feasibility test is done. budget-wise. then prototype is given to client step by step. Changes are noted.
In this whole process, the use of OOP is beneficial. Maintaining, updating and carry forward existing versions to next version can be done easily using OOP. Code flexibility is enhanced. We can make changes to the project without much changes in code using OOP. Also, extension of code becomes very easy. We can add new features without much changes to the old ones. This is why in a popular language like Python, learning about OOP is important.
file = open("myfile.txt", "r")
Opens the file. We can read the contents of the file using the file.read()
method. We can write into a file using the file.write("\n")
method. def functionname (classname,......arguments......):
...
...
...
self
) is a must. Private methods can be made by writing two underscores before the class name. Private methods cannot be accessed from outside the class.__init__
keyword. A constructor is a special type of member function of a class which initializes objects of a class. The constructor is run when the class is instantiated. The class constructors can be called by other parts of the class or even outside pf the class. If we do not provide a constructor to the class, Python generates one default constructor automatically. We can even pass parameters to the constructors.