0

I have read conflicting accounts on whether explicit imports were required for read only usage. On all accounts the imports are required for any mutations to globals.

Specifically I am annoyed by needing to put global ... inside every method within a single script for such things as Enums that are always invariant. I'd like a program-level setting to just allow read-only usage of _globals.

What are the options here?

Update I see what is happening: the Pycharm is actually only flagging these when there is a mutation happening. It was hard to discover because often enough the first usages of the global variable were read-only : but only further down in a given method were the mutations happening. In the case shown in the screenshot it is the lastBadgeId:

enter image description here

If the mutations in subsequent code in that same method are commented out then the red goes away.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

1 Answers1

1

There is no need to declare a global for a variable which you only read. In a complex program, this may help the reader; but there is no technical necessity. A more useful convention anyway is to use upper case for globals.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    Identifiers with all capital letters are being used for **constants** (which may or may not be global). There is no special capitalization for global variables in the Python standards. See https://peps.python.org/pep-0008/#constants https://peps.python.org/pep-0008/#global-variable-names – pabouk - Ukraine stay strong May 30 '22 at 07:27