0

I have to create a global variable.

this variable is initialized in the main after an input parameter is passed and then I want to make it available everywhere in the code without passing it in the init of the different objects.

global pulse
def main(shot_no,...):
    pulse = shot_no


    #create object
    signal = Data(constants)
    print('here')
    print(pulse)


    success = signal.read_data(shot_no,...)

within the class Data pulse is defined in the init as global.

Data is a class inherited from another and also in this one I define in the init pulse as global

I get an error "NameError: name 'pulse' is not defined"

any help?

the answer Python variable scope error

doens't answer my question (as a matter of fact there is no actually an answer in that post)

bruvio
  • 853
  • 1
  • 9
  • 30
  • Define `pulse` outside and refer to it inside as `global pulse`. – Santosh Kumar Oct 12 '18 at 11:32
  • how? can you make an example pleasE? – bruvio Oct 12 '18 at 11:34
  • You need to put `global ...` **into every function** that is trying to access a global variable. Or, better, if you're working with a class anyway, set it on `self`, and/or pass it as a parameter and `return` it. `global` is generally terrible practice. Function scope is not something you need to work around. It exists for a reason, to keep your code compartmentalised and maintainable. Work *with* it, not against it. – deceze Oct 12 '18 at 12:05

0 Answers0