-3

I have this simple code, I want value of 'a' to be 4 at the end. 'global a' can solve it, but is there any other way? Since it's everywhere that overuse of globals is a symptom of poor design.

a=3
def function():
    a = 4

function()
print(a)
martineau
  • 119,623
  • 25
  • 170
  • 301
barry23
  • 9
  • 2
  • 3
    You've already used a global variable as soon as you've written `a=3`. – Ignacio Vazquez-Abrams Apr 15 '18 at 00:34
  • 1
    The title asks "how to avoid global variables", and the question asks "how to modify a global variable inside a function without `global` keyword". You should explain what is the problem you are trying to solve. – Gabriel Apr 15 '18 at 00:42
  • 1
    It's hard to say the "right" way to write something like this, because if we try to fix the things that don't make sense, it rapidly simplifies to `print(4)`. – user2357112 Apr 15 '18 at 00:44
  • "how to modify a global variable inside a function without global keyword" Yes, thats pretty much the question, thanks – barry23 Apr 15 '18 at 00:46
  • 2
    Why do you want to avoid the `global` keyword? What is the underlying problem you're trying to solve? – Gabriel Apr 15 '18 at 00:47
  • Okay, what i try to achieve here: 'function' is bind to a button in tkinter that calculates a new value for 'a' from whatever other parameters. After it's pressed i want to use 'a' with the new calculated value. Since you can read everywhere that overuse of globals is a symptom of poor design, question is, is it poor design and can be a better way to make this work? – barry23 Apr 15 '18 at 01:14
  • 1
    Knowing your goal is important to know. `tkinter` has special classes called "Variables" (see [The Variable Classes (`BooleanVar`, `DoubleVar`, `IntVar` `StringVar`)](http://effbot.org/tkinterbook/variable.htm)) that make what you want to do easy and avoids global variables. Also see [Control variables: the values behind the widgets](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/control-variables.html) for more details. – martineau Apr 15 '18 at 01:34

2 Answers2

-1

If you want a to be 4 you have to return it from the function then reassign it to a.

a=3
def function(b):
    b = 4
    return b

a = function(a)
print(a)

Any names inside of a function are local to that function and as such you can perform a calculation inside the function independent of the rest of the code.

Once this is complete you can reassign back to the variable for further processing.

So maybe not just turn it to 4, you could do something like:

a=3
def function(b):
    b = b ** 4 // 5
    return b

a = function(a)
print(a)
johnashu
  • 2,167
  • 4
  • 19
  • 44
-1

Figured out a solution, that stores the variable in a class:

class aa:
     def __init__(self):
          self.a=3

variables = aa()

print(variables .a)  # Prints '3'

def function():
    z.a = 4

function()

print(variables .a)  # Prints '4'
martineau
  • 119,623
  • 25
  • 170
  • 301
barry23
  • 9
  • 2
  • 1
    That's a hack, not a solution. – Gabriel Apr 15 '18 at 01:36
  • 1
    You don't need to define a class in order to do something like this. See the [question](https://stackoverflow.com/questions/19326004/access-a-function-variable-outside-the-function-without-using-global) this one has been marked a duplicate of. – martineau Apr 15 '18 at 01:38