The following is a defined class in Python. Python uses the naming convention _name_ for internal functions. Python allows operator overloading, e.g. you can define what the operator
+ will do for a specific class.
|_init_||Constructor of the class|
|_str_||The method which is called if print is applied to this object|
The empty object (null) is called
_None_ in Python.
class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __str__(self): return "x-value" + str(self.x) + " y-value" + str(self.y) def __add__(self,other): p = Point() p.x = self.x+other.x p.y = self.y+other.y return p p1 = Point(3,4) p2 = Point(2,3) print (p1) print (p1.y) print (p1+p2)
x-value3 y-value4 4 x-value5 y-value7
If you like dEexams.com and would like to contribute, you can also write your article here or mail your article to firstname.lastname@example.org . See your article appearing on the dEexams.com main page and help others to learn.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.