How to avoid using a global variable?

Richard

Getting back into programming after a 20 years hiatus. Been reading that the use of global variables in python is a sign of bad design, but can't figure out a better way of doing it.

Below is a small program that utilizes a global variable 'paused' to determine the state of the music player. This variable is utilized by a couple of functions.

Is there a better way of doing this without utilizing a global variable?

# Global variable to access from multiple functions
paused = False


def play_music():

    global paused

    if not paused:

        try:
            mixer.music.load(filename)
            mixer.music.play()
            statusBar['text'] = 'Playing Music - ' + os.path.basename(filename)
        except:
            tkinter.messagebox.showerror('File not found',
                                         'Melody could not find the file.')
    else:
        mixer.music.unpause()
        paused = False
        statusBar['text'] = 'Playing Music - ' + os.path.basename(filename)


def stop_music():
    mixer.music.stop()
    statusBar['text'] = 'Music stopped'


def pause_music():

    global paused

    if not paused:
        mixer.music.pause()
        paused = True
        statusBar['text'] = 'Music paused'

    else:
        play_music()
Óscar López

You could put all your functions inside a class, and make the "global" variable an attribute. In that way you can share it between methods:

class Player(object):
    def __init__(self):
        self.paused = False
    def play_music(self):
        if not self.paused:
            # and so on
    def pause_music(self):
        if not self.paused:
            # etc.

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 using a global variable in this recursion function and improve my code?

PHP Recursive Function - How To Avoid Using A Global Variable

How would I avoid using a global variable here?

How to avoid declaring a global variable?

How to avoid using global variables?

How to avoid global variable and repetition in python

How to avoid using a global pointer variable in my GTK3 Application

How to avoid global variable declaration when using Perl's dynamic scoping?

Convert Binary Tree to doubly linked list. How can I avoid using global variable here?

How to avoid using global variables in javascript slider

Avoid using global variable in Java 8 stream reduce method

Why is a global `name` variable declared in typescript and can I avoid using it?

What is the best practice to avoid using static global variable?

java - how to avoid global external variable as output in recursive function

How might I avoid a global mutable variable in this code?

How to avoid mutating original global variable when creating copy

Proper way to avoid global variable

How to avoid using temporary variable with std::modf?

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

How to avoid global variables when using interrupt handlers?

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

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

How to set a global variable using MySQLContainer

How to change a global variable in a function using parameters

How to create a global variable using user input

How to global variable access in function using Javascript?

How to change global variable using local variables

how to disconnect socket using the global io variable

How to declare global variable using Set-Variable in PowerShell?