Home > Enterprise >  function in function in class is not capable of getting variable in the first function. works fine w
function in function in class is not capable of getting variable in the first function. works fine w

Time:06-20

I have a program written like this

class test:
   def test2():
      def test3():
          global tester
          print(tester)
      tester = 'working'
      test3()
test.test2()

I am fully capable of using the script as is without the class and cannot remove the class and the current structure because I am referencing this class in another script which is threaded as a separate instance.

The only and main problem is the inability to get the global function to work for a function in a function in a class.

CodePudding user response:

Seems like every time I can't find an answer whenever I ask 5 seconds later I found the solution.

class test:
    def test2():
        global tester
        def test3():
            global tester
            print(tester)
        tester = 'working'
        test3()
test.test2()

this works added tester as a global in what normally would be called reference before assignment.

  • Related