Home > database >  I have a question about modifying variables in OOP
I have a question about modifying variables in OOP

Time:04-30

I have this file called mainClass.py:

summed = 3
class myClass:
   def test1(self):
   print(summed)
   summed = 5

I can print the summed variable, but I can't modify it. Why?

CodePudding user response:

summed at the main level is a global variable. If want to access the global variable then add global summed to the function.

In the posted function, printing summed followed by a local assignment will result in an error: UnboundLocalError: local variable 'summed' referenced before assignment since the variable in the function has become a local variable due to the assignment statement summed = 5. For further explaination, refer to the FAQ.

summed = 3'

class myClass:
   def test1(self):
     global summed
     print('  test1:', summed)
     summed = 5 # updates global summed variable

   def test2(self):
     summed = 10 # this is a local summed variable
     print('  test2:', summed)

print("main:", summed)
a = myClass()
a.test1()
print("main:", summed)
a.test2()
print("main:", summed)

Output:

main:  3
  test1: 3
main:  5
  test2: 10
main:  5

After calling test1() function, summed value is changed to 5 but calling test2() changes a local summed variable not the global variable so value remains 10.

CodePudding user response:

The summed variable you are printing is the global summed which is 3, if you want to print the class summed variable (or modify it) you should added self. to the variable like this:

summed = 3
class myClass:
   def test1(self):
       print(self.summed)
   summed = 5

Now if you modify summed like this

c = myClass()
c.summed = 6
c.test1()

it will print 6. In case you want to change the global summed variable, just add global keyword to it like this: summed = 3

class myClass:
   def test1(self):
       print(self.summed)
   global summed
   summed = 12

This will change the value of the global variable.

CodePudding user response:

python thinks it is a local variable and produces UnboundLocalError: local variable 'summed' referenced before assignment you should change your code to

summed = 3
class myClass:
   def test1(self):
     global summed
     print(summed)
     summed = 5
     
myClass().test1()
print(summed)

It would now think summed as a global variable and print

3
5

CodePudding user response:

If a function assigns to a variable*, then that variable is assumed to be local, even if there is a global variable of the same name.

If you want the function to use the global variable, you have to use the global keyword.

def test1(self):
    global summed
    # all references to 'summed' in this function will use the global variable
    ...

*This is true even if the assignment is in a branch that doesn't execute -- even in a branch that can't execute!

For example:

A = 5

def foo():
    if False:
        A = 4
    else:
        print(A)

foo()

This code will throw an exception at print(A).

  • Related