furas

Python - class example - (FB: learnpython.org)

Jan 2nd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. # define class
  2. class Calc:
  3.    
  4.     def __init__(self, a, b):
  5.         self.x = a
  6.         self.y = b
  7.  
  8.     def add(self):
  9.         return self.x + self.y
  10.  
  11.     def sub(self):
  12.         return self.x - self.y
  13.  
  14.     def mul(self):
  15.         return self.x * self.y
  16.  
  17.     def div(self):
  18.         return self.x / self.y
  19.  
  20. # create instance
  21. my_calc = Calc(6, 3)
  22.  
  23. # use methods of instance
  24. print(my_calc.add()) # 9
  25. print(my_calc.sub()) # 3
  26. print(my_calc.mul()) # 18
  27. print(my_calc.div()) # 2
Add Comment
Please, Sign In to add comment