-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
  • 112,593
  • 23
  • 157
  • 280
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,099
  • 3
  • 17
  • 37
  • 1
    Any explanation for the downvote? – johnashu Apr 15 '18 at 00:40
  • 4
    It's probably because this code doesn't make much sense. – Gabriel Apr 15 '18 at 00:45
  • @Gabriel Does not make sense in what way? – johnashu Apr 15 '18 at 00:50
  • 1
    It did not make sense until you edited it (not the downvoter(s)) – cs95 Apr 15 '18 at 00:51
  • You created a local variable `a` inside the function, which has the same name of a global variable `a` --kind of pretending that the local `a` is the same as the global `a`, then you updated the global `a` with the return value of the function, which happens to be the local `a` Wow. – Gabriel Apr 15 '18 at 00:56
  • @Gabriel That is perfectly fine in python.. As i mentioned the a inside the function is local to that function. . I have changed it to b's to avoid any confusion.. I was not pretending anything.. I said it needed to be returned and then reassigned to a.. The naming means nothing – johnashu Apr 15 '18 at 01:00
  • 1
    I understand your program is a valid Python program, it was misleading tho. Anyway, your edits improved it. – Gabriel Apr 15 '18 at 01:02
  • @Gabriel Yes, I understand!.. I will give a little more detail – johnashu Apr 15 '18 at 01:03
-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
  • 112,593
  • 23
  • 157
  • 280
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