Skip to content

Files

Latest commit

 

History

History
33 lines (22 loc) · 728 Bytes

global-statement.md

File metadata and controls

33 lines (22 loc) · 728 Bytes

Pattern: Use of global statement

Issue: -

Description

Global variables reduce code readability and often cause bugs. Avoid global variable as much as possible.

Example of incorrect code:

WIDTH = 0 # global variable

def area(w):
    global WIDTH # global statement
    WIDTH = w
    return WIDTH * WIDTH

One common solution for this is to create a class and to encapsulate global variables as members of an instantiated object of that class:

class Square:
    def __init__(self, width):
        self.width = width
    def area(self):
        return self.width * self.width

Further Reading