Given the following in the same script:
class ClassA:
def GetExpBidBrice():
global x
x ='abc'
def GetExpAskPrice():
y = x + 'zyz'
class ClassB:
def GetExpBidBrice():
x = 123
def GetExpAskPrice():
y = x + 'zyz'
Does the global in ClassA.GetExpBidPrice() make x global just to the other method in ClassA? Or does it make it global right across the board i.e. in ClassB or any other class that uses a variable called x?
The variable is made global in the current code block only. From the
globalstatement documentation:The variable
xin other methods is unaffected. It’ll only be a global in theClassA.GetExpBidBrice()method.If you wanted it to be a global in the other methods as well (
ClassA.GetExpAskPrice(),ClassB.GetExpBidBrice()andClassB.GetExpAskPrice()) then you’d need to declarexa global in each method separately.