PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] When a function is called in another function why does global not function as it should?

 July 07, 2022     class, function, global     No comments   

Issue

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.


Solution

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.



Answered By - Brecht Corbeel
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing