Python Forum
Global variables not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global variables not working
#9
(Jan-15-2021, 03:05 PM)deanhystad Wrote: Do you understand what global does? Do you understand the concept of scope?

You can think of scope as a kind of dictionary that stores names. When you use a variable Python searches for the name in these scope dictionaries. Every function has a scope. Every module (file) has a scope. Python has a scope. The scopes even have names; Local for the function scope, Global for the module scope, and Built-in for the Python scope. There is an additional scope called Enclosing which occurs if you define functions inside of functions.

When you use a variable in your code Python searches for the variable in these scopes. The order of the search is Local, Enclosing, Global, Built-in. If you read about scope you may see this order referred to as LEGB. This is why ndc85430 asked why you are using the global keyword to use a mutable variable. Global variables are visible for use in any function defined in the module. However ndc85430 is wrong about "sentence_1" being mutable. "sentence_1" is a string, and strings are not mutable in Python.

When you assign a variable in your code Python creates the variable in the current scope. If your function uses the "global" keyword to declare the variable, the assignment creates the variable in the global scope. Your code would work if you got the syntax correct.
        def function_A:
            # do something ...
            global sentence_1
            sentence_1 = "new string 1"
You would know this if you ever bothered to read the Python documents before using new language features. If all your learning is focused on tutorials or looking at examples you risk never learning about how things work and why they work this way. The Python documents explain how a language feature fits into the Python puzzle. If you start reading the docs not only will you have fewer questions, but you will get a deeper understanding of Python and grow an intuition about why something is not working.

buran's suggestion about using attributes is a good one. In a class not only can you think of a scope as being a dictionary, it is a dictionary in the literal sense. An attribute is a variable defined in the context of the class. You use a special syntax to access these variables. From inside the class you use "self.name" to access the "name" attribute. From outside the class you use "instance.name" where "instance" is the name of a variable that references an instance of the class.
class MyClass:
    def __init__(self):
        """Use docstring notation instead of # as the header comment"""
        self.sentence_1 = "Hello"
        self.sentence_2 = "World"
 
    def function_A(self):
        # do something ...
        self.sentence_1 = "Goodnight"
 
    def function_B(self):
        # do something ...
        self.sentence_2 = "moon"

    def __repr__(self):
        return f'{self.sentence_1} {self.sentence_2}!'

a = MyClass()
print(a)
print(a.sentence_1)
print(a.sentence_2)
a.sentence_2 = 'sun'
print(a)
a.function_A()
a.function_B()
print(a)
The most useful thing about classes is that each instance gets their own namespace that they carry around wherever they go. If I create another instance of MyClass and assigned to the variable "b", I now have two instances of the class that are independent of each other. Changing attributes in "a" does not affect the corresponding attribute in "b". This is something that would be very difficult to do using just functions and modules.
Output:
Hello World! Hello World Hello sun! Goodnight moon!

Many thanks, it finally works!!

Also thanks to everyone else who responded and helped me !!
Reply


Messages In This Thread
Global variables not working - by hobbyist - Jan-15-2021, 12:55 PM
RE: Global variables not working - by ndc85430 - Jan-15-2021, 01:04 PM
RE: Global variables not working - by hobbyist - Jan-15-2021, 01:16 PM
RE: Global variables not working - by buran - Jan-15-2021, 02:34 PM
RE: Global variables not working - by deanhystad - Jan-15-2021, 03:05 PM
RE: Global variables not working - by hobbyist - Jan-15-2021, 09:01 PM
RE: Global variables not working - by buran - Jan-15-2021, 03:16 PM
RE: Global variables not working - by ndc85430 - Jan-15-2021, 04:13 PM
RE: Global variables not working - by deanhystad - Jan-15-2021, 04:42 PM
RE: Global variables not working - by jefsummers - Jan-16-2021, 03:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,144 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,041 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,163 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 963 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,500 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Global vs. Local Variables Davy_Jones_XIV 4 2,676 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,282 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Global not working why? Milfredo 13 4,458 Oct-06-2020, 02:37 AM
Last Post: Milfredo
  Question regarding local and global variables donmerch 12 5,130 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,456 Apr-08-2020, 06:01 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020