Quantcast
Channel: How to avoid using global variables? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

How to avoid using global variables?

$
0
0

I use global variables but I've read that they aren't a good practice or pythonic. I often use functions that give as a result many yes/no variables that I need to use in the main function. For example, how can I write the following code without using global variables?

def secondary_function():    global alfa_is_higher_than_12    global beta_is_higher_than_12    alfa = 12    beta = 5    if alfa > 10:        alfa_is_higher_than_12 = "yes"    else:        alfa_is_higher_than_12 = "no"    if beta > 10:        beta_is_higher_than_12 = "yes"    else:        beta_is_higher_than_12 = "no"def main_function():    global alfa_is_higher_than_12    global beta_is_higher_than_12    secondary_function()    if alfa_is_higher_than_12=="yes":        print("alfa is higher than 12")    else:        print("alfa isn't higher than 12")    if beta_is_higher_than_12=="yes":        print("beta is higher than 12")    else:        print("beta isn't higher thant 12")main_function()

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images