Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var=1
- class ClassName():
- def __init__(self):
- # ...
- #some other methods that do not need global variable here
- def Met():
- #do some other stuff that needs the global var
- """ Approach one """
- var=1
- class ClassName():
- def __init__(self):
- # ...
- #some other methods that do not need global variable here
- def Met():
- # needs the global var to operate
- global var
- # do some stuff with var (including editing)
- """ Approach two"""
- var=1
- class ClassName(var):
- def __init__(self):
- # ...
- #some methods that do not need global variable here
- def Met(var):
- # do some stuff with var (including editing)
- var=1
- def func1():
- #do some stuff
- #do not need global variable here
- def func2():
- #do some other stuff
- # need the global var
- func2() # call func2
- var=1
- def func1():
- def func2():
- global var
- var = 2 # global variable updated
- func2()
- func1()
- print(var)
- var=1
- def func1(var):
- def func2(var):
- var = 2
- print(var) # local var is printed, global var unchanged
- func2(var)
- func1(var)
- print(var) # global var is printed
Add Comment
Please, Sign In to add comment