0

In the following .py program, n is used within countingFunc but doesn't have to be included among its arguments, because, we might say, "n doesn't change."

n = 6
k = 0

def countingFunc(k):
    if k < n:
        k += 1
        print(k)
    else:
        k = n

countingFunc(k)

However, there can be many ways in which a variable may or may not change in the functions of a module. For simple cases that include expressions that change n but that could never execute (like below), the compiler complains that n is referenced before it is assigned.

n = 6
k = 0

def countingFunc(k):
    if k < n:
        k += 1
        print(k)
    else:
        k = n
    
    if k > n:
        print('A miracle has occurred')
        n += 1

countingFunc(k)

This raises the question of what criterion the compiler uses to determine whether a variable can be omitted from the arguments of a function. (Since we want code to be portable across compilers, I would imagine that this criterion is the same or very similar for most compilers, but this is just a guess.)

With the exception of cracking open compiler source code, I've dug through online resources looking for an answer but without success. There are good tutorials on passing variables like this one and this one, and there is interesting information in the answers of related Stack Overflow questions such as this one. However, I'm still looking for a direct answer to the above question.


NOTE: As mentioned by furas (B. Burek) in the comments, best practice in Python (and many other languages) is to explicitly include all used variables as arguments and not omit them. This question merely observes that standard Python interpreters do allow certain variables to be omitted from function arguments according to some criterion and asks what this criterion is.

SapereAude
  • 134
  • 6
  • some variables never change so in all functions they will have the same value and you may not have to send them as argument, the same is with returning, If you don't change `window` then you don't have to return it. Besides `button` doesn't know what to do with returned value - so `return` will be useless. In your example when you send `k` to function then it duplicate value and create local `k` which has nothing to do with external `k` - this way this value is safe inside function and when other function change it outside functions then inside function you will have still the original value – furas Jan 06 '21 at 22:06
  • peferred rule is to send all values which you use inside function - `command=lambda: update_gui(k,-1,captions, label, fig, window, btn1, btn2)` - this way everyone can see what is used in function even if this function is in different file and it is hard to find it. It helps to understand function and debug it. But sometimes we use directly external variables because it need less code to write and we are lazy. – furas Jan 06 '21 at 22:15
  • you don't have to return `window` because you don't assign new value directly - `window = ...`. You change only values inside `window` like `window.axes1, window.axes2` but it doesn't change value directly assigned to `window` - it still refers to the same same space in memory. – furas Jan 06 '21 at 22:20
  • @furas Thanks for this explanation. Based on your comments, I've changed the question dramatically to eliminate the focus to Tkinter. – SapereAude Jan 07 '21 at 01:48
  • general rule is to use all variables in arguments and not ommit them. If you run `import this` then you see ["The Zen of Python"](https://www.python.org/dev/peps/pep-0020/) and one of rule is `Explicit is better than implicit.`. If you send all used variables as arguments then you have `"explicit"`, if you skip some arguments then you have `"implicit"`. And it doesn't matter if you will change value in this variable or not. If you use external variable (even only to get value from variable) then send it as argument. – furas Jan 07 '21 at 08:10
  • @furas Thanks. Now noted in the question itself. – SapereAude Jan 07 '21 at 14:03
  • there is no special rule - there are only lazy people which want to write code faster, so they skip some arguments because they know it will still work (until they get error) – furas Jan 07 '21 at 14:18

0 Answers0