code_junkie

A small question about python's variable scope

Nov 12th, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. def hello(x,y):
  2. good=hi(iy,ix)
  3. "then do somethings,and use the parameter'good'."
  4. return something
  5.  
  6. def hi(iy,ix):
  7. "code"
  8. return good
  9.  
  10. def hello(x,y):
  11. fordf150 = hi(y,x)
  12. "then do somethings,and use the variable 'fordf150'."
  13. return something
  14.  
  15. def hi( ix, iy ):
  16. "compute some value, good."
  17. return good
  18.  
  19. varA = 5 #A normal declaration of an integer in the main "global" namespace
  20.  
  21. def funcA():
  22. print varA #This works, because the variable was defined in the global namespace
  23. #and functions have read access to this.
  24. def changeA():
  25. varA = 2 #This however, defines a variable in the function's own namespace
  26. #Because of this, it's not accessible by other functions.
  27. #It has also replaced the global variable, though only inside this function
  28. def newVar():
  29. global varB #By using the global keyword, you assign this variable to the global namespace
  30. varB = 5
  31.  
  32. def funcB():
  33. print varB #Making it accessible to other functions
  34.  
  35. def hello(x,y):
  36. good=hi(iy,ix)
  37. "then do somethings,and use the parameter'good'."
  38. return something
  39.  
  40. def hi(iy,ix):
  41. "code"
  42. return great
Add Comment
Please, Sign In to add comment