Advertisement
here2share

# local_vs_global.py

May 21st, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. # local_vs_global.py
  2.  
  3. class Car(object):
  4.     __maxspeed = 0
  5.     __name = ""
  6.  
  7.     def __init__(self):
  8.         self.__maxspeed = 250
  9.         self.__name = "Supercar"
  10.  
  11.     def drive(self):
  12.         print 'global/hwy driving: maxspeed ' + str(self.__maxspeed)
  13.  
  14.     def change_maxspeed(self, limit):
  15.         self.__maxspeed = limit
  16.  
  17. redcar = Car()
  18. redcar.drive()
  19. redcar.__maxspeed = 80  # will not change variable because its private
  20. redcar.drive()
  21. print 'local driving:', redcar.__maxspeed # local
  22. redcar.change_maxspeed(40)
  23. redcar.drive()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement