Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(python) Should my variable be local or global? (best practice)

When declaring a constant that is only used one function, should that variable be declared locally since it is only used by that function, or globally since it is never going to change?

IE which is better:

CONSTANT = (1, 3, 5, 8)

##SOME OTHER CODE HERE

def function1(arg):
    if arg in CONSTANT:
        do something

or:

def function1(arg):
    CONSTANT = (1, 3, 5, 8)
    if arg in CONSTANT:
        do something

I know there isn't a lot of difference between these two, but I just wanted to know which of the two practices is preferred since I'm just starting out and want to form good habits.

like image 663
user2236076 Avatar asked Apr 06 '13 12:04

user2236076


People also ask

Is it better to use local or global variables?

It all depends on the scope of the variable. If you feel that a certain variable will take multiple values by passing through various functions then use local variables and pass them in function calls. If you feel that a certain variable you need to use will have constant value, then declare it as a global variable.

Is it good to use global variable in Python?

The use of global variable in python is considered bad practice and should generally be avoided. Instead the user can try and use a parameter for passing a value onto a function or return a value to obtain it.

Is it good practice to use global variables?

Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.

Why would you use a local variable instead of making everything global?

So, by using a local variable you decrease the dependencies between your components, i.e. you decrease the complexity of your code. You should only use global variable when you really need to share data, but each variable should always be visible in the smallest scope possible.


2 Answers

I would keep it local. You can always just move it global in the future if you need to, or share it between functions by making them methods in a class and turning the constant into a class variable. In these situations, generally speaking, the more local, the better, and best is to hide implementation information within your functions, as in your second example. It doesn't make a huge difference here, but as your projects get bigger, maintainability and modularity will be sustained.

like image 98
acjay Avatar answered Sep 28 '22 02:09

acjay


I would put them global because:

  1. Your variables are constants
  2. In Python, global scope is encapsulated in the module namespace, meaning that your variable is in fact only global inside the module.
  3. If you call your function a lot of times, and put your constants local to it, it would reallocate them each time your call the function.
  4. Then you can share your constants between different functions.

However, if you move to Object Oriented Programming, then I would put the constants as class variables.

like image 36
Charles Brunet Avatar answered Sep 28 '22 04:09

Charles Brunet