0

Globals.py

dynamicValue = ''

staticValue = 'This is a string'

Controller.py

import Globals.py

print('The static value is: %s' % Globals.staticValue))
>> The static value is: This is a string

someValue = 'i can assign this value to dynamicValue'

Globals.dynamicValue = someValue

print('The dynamic value is: %s ' % Globals.dynamicValue))
>> The dynamic value is: i can assign this value to dynamicValue

I'm able to maintain all my variables that I will need to use across various methods and files this way and it has been working like a charm. I was wondering why I don't see this approach mentioned though.. Is this bad practice?

Pablo H
  • 147
  • 1
  • 2
  • 7
  • Using Global variables (having side-effects) is not something specifically related to python, it related to the programming paradigm you are following. Python is multi paradigm, for example, you may see the functional and oop paradigm in python used in the web context, while you may say the imperative one used in data-science or visualizations. – adnanmuttaleb Mar 18 '21 at 21:30

1 Answers1

2

This is a bad practice across any programming language, not only Python. Global variables tend to create undesirable side-effects.

Always remember: Being able to do it doesn't mean you should do it.

enbermudas
  • 1,603
  • 4
  • 20
  • 42