I would like to do something similar, but I have SyntaxWarning and it does not work as expected
RAWR = "hi" def test(bool): if bool: RAWR = "hello" # make RAWR a new variable, don't reference global in this function else: global RAWR RAWR = "rawr" # reference global variable in this function print RAWR # if bool, use local, else use global (and modify global)
How do I make this work? Passing in True or False changes the global variable.
. , (, ) . . global RAWR RAWR ( , , ), , . Edit: veredesmarald , Python 2. Python 3.
global RAWR
RAWR
-, , "" , . ( .)
, , -
RAWR = "hi" def test(newone): if newone: lR = "hello" # make RAWR a new variable, don't reference global in this function else: global RAWR lR = RAWR # reference global variable in this function print lR # if bool, use local, else use global (and modify global) # modify lR and then if not newone: RAWR = lR
, , .
class store_RAWR(object): RAWR = "hi" def __init__(self, new): self.RAWR = new def test(newone): if newone: myR = store_RAWR("hello") # get a (temporary) object with a different string else: myR = store_RAWR # set the class, which is global. # now modify myR.RAWR as you need
, .