2

according to my understanding, in python, if i want to change/write the value of a global variable in a function, i should firstly and locally make a global declaration of this global variable in the function, whereas if only access/read the value of this global variable, it's no need to make the global declaration, as like the below simple code, which works as expected:

# python3
global_var = 10

def read_global_var():
    print(global_var) # the global var is successfully read without global declare
    

def write_global_var():
    global global_var # global declare is must before write
    global_var = 5



print(global_var)
read_global_var()
write_global_var()
print(global_var)

but when comes to the instance of python class, in a similiar usecase, i just found global statement looks not necessary, which makes me confused, as in the following example:

# python3
class C:
    def __init__(self):
        self.x = 1.0


def read_global_instance():
    print(global_instance.x)
    

def write_global_instance():
    # global instance # this global declare is not nessesary, looks no different with or without this 
    global_instance.x = 3.0 
    # not like "global var", the "global instance" does not global declare before write


global_instance = C()
print(global_instance.x)
read_global_instance()
write_global_instance()
print(global_instance.x)

can i get any explanation about this inconsistent, at least to me, the two examples above are the same use-case.

furynerd
  • 109
  • 1
  • 8
  • *objects don't have a scope, variables have a scope*. Instances are neither global nor local, scope does not apply to instances. And *everything* in Python is an instance (including ints etc) – juanpa.arrivillaga Nov 17 '22 at 10:27
  • "but when comes to the instance of python class..." **your first example involved an instance of a python class** – juanpa.arrivillaga Nov 17 '22 at 10:28
  • A `global` statement is necessary to assign **to a global variable**. `global_instance.x = 3.0` does not assign to a global variable. You only ever *reference* the variable `global_instance`, you *never assign to that variable* so the global statement is not necessary. The **type of object involved is irrelevant to how this works** – juanpa.arrivillaga Nov 17 '22 at 10:28
  • "global_instance" variable itself is only read in the functions, never set. Setting an attribute of an instance is a different operation. – Michael Butscher Nov 17 '22 at 10:29
  • i agree **everything in python is an instance**, and in fact my **first example involved an instance of a python class**, but that is just why i am confused by the inconsistent between the two examples – furynerd Nov 17 '22 at 10:39
  • @furynerd because *you are doing two completely different things*. Your examples are not inconsistent at all. As explained, `global_instance.x = 3.0` *does not assign to a variable*, it sets an attribute on an object. It is using a *local variable* to do that – juanpa.arrivillaga Nov 17 '22 at 10:41
  • so in the first example, if the `global_var.real` is writable, then the `global` statement would not be needed as the second example? it looks convinced to me, so in first e.g. i am `initialize` a `int-class`, and second e.g. i am `set attribute` of a class... – furynerd Nov 17 '22 at 10:51
  • so if this is the gramma of python and how it should be compliant....then i got it...though still a little loose feeling of the gramma about python, but thx very much – furynerd Nov 17 '22 at 10:56

0 Answers0