How to avoid using global variables?

Carl

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()
sjc

One could ask what reasons you might have to structure your code like this, but assuming you have your reasons, you could just return the values from your secondary function:

def secondary_function():

  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"

  return alfa_is_higher_than_12, beta_is_higher_than_12


def main_function():

  alfa_is_higher_than_12, 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")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to avoid global variables in JavaScript?

How to avoid global variables when using interrupt handlers?

Avoid using global variables when using recursive functions in C

How to avoid class and global variables

Is this strategy, to avoid global variables in C, right?

Using Global Variables in VBA

The best way to avoid using lots of global variables or make them easy to access and modify

How to avoid declaring global variables in this case?

How to avoid using a global variable?

How to avoid using a global variable in this recursion function and improve my code?

How to declare timer while avoid global variables?

Avoid global variables but keep code easy to understand

Shiny:How to avoid global variables and problem with ggplot one observation at a time

How to build this program without using global variables?

How do you avoid using global variables in inherently stateful programs?

Maxima: Force a function to run with local-variables only? / How to Avoid Local Operations Affecting Global-Variables?

How I can avoid using "global" with asyncio (telethon)?

Can I avoid using global variables in python while coding a bot with discord.py?

How to avoid global variables in Durandal 2

Using global variables in a library

How to avoid resetting global variables in a js file

How to avoid using global variables in javascript slider

How can I avoid global variables in a Threaded application

How to change global variable using local variables

PHP Recursive Function - How To Avoid Using A Global Variable

How to avoid using global/class level variables in recursion?

Using global variables in PHPMailer

How would I avoid using a global variable here?

How to avoid using global variables when coding a game in C with the betty style?