Advertisement
Kuipo

class example (python)

Mar 24th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1.  
  2.  
  3. class MyClassObject:
  4.     def __init__(self):
  5.         # this function must be in any class and will run when instantiated
  6.         self.var = 100
  7.  
  8.     def print_value(self):
  9.         # all class methods (functions) must have their first argument be 'self'
  10.         print self.var
  11.  
  12.     def add_10(self):
  13.         self.var = self.var + 10
  14.  
  15.  
  16. a = MyClassObject()
  17. a.print_value() # expect 100.  this will call the print_value method of the class
  18. a.add_10()
  19. a.print_value() # expect 110
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement