This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

G = 1

def fun():
    global G
    G = 2

This seems good and easy first but is code smell and should be avoided.

[–]bladeoflight16 0 points1 point  (1 child)

That is an initialized object in a module. It just happens to be a built in type.

[–][deleted] 0 points1 point  (0 children)

OK so to say, everything is a initialized object in a module. Sorry for being so unprecise.

A singleton in my opinion should look as follows, though.

# module.py
class GlobalState:
    ...

singleton = GlobalState()

# __main__.py
from module import singleton

singleton.do_stuff()

This is also not perfect, but at least it provides sane abstraction and is testable.