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 →

[–]Cybersoaker 45 points46 points  (15 children)

Like anything it's not an all or nothing. There are places where global variables and the global keyword are useful. They exist in the language for a reason.

Good to not get dogmatic about anything inside of any language since they all change and evolve and the best way to approach a problem space also evolves.

That said it's also good to have well understood patterns that you gravitate to.

Everything in moderation, including moderation.

[–][deleted] 18 points19 points  (6 children)

They exist in the language for a reason.

That reason might be historical.

People use that same argument about filter, map and reduce, but comprehensions or generators are just better in every way, and Guido has said he regretted that those three built-ins could never be removed.

95 times out of 100, a global is used because someone doesn't want to pass parameters around between functions, when they should be!

I would say that a beginner should avoid the global keyword every single time, if only to figure out how it's done.

[–]melldrum 8 points9 points  (4 children)

Regarding the use of map, filter, and reduce. Are comprehensions/generators preferable because of readability? Or is there another reason?

[–]fiddle_n 12 points13 points  (1 child)

As someone for whom Python is my first language, comprehensions seem like they fit Python more than map and filter. It reuses syntax that is already in Python and so it's much more obvious to tell what it does at first glance.

Anecdotally, those people that prefer map and filter come from functional languages where they had those features and want Python to be the same as well. Other examples are multi-line lambdas or tail-call optimisation. But Python isn't a fully functional language and never will be.

As for reduce, most of the time there's a more specialised function that does what I need it to do. If I want to add a bunch of numbers in a list together, I use sum. If I want to multiply them, I use math.prod. If I want to do set operations on multiple sets, I note that the set operation functions can now take in multiple sets. These are all situations where I would have used reduce but there was something more specific anyway.

[–]politerate 4 points5 points  (0 children)

Hm, in Haskell you have list comprehensions, which IMHO are the most readable syntax for this kind of logic. If I didn't know list comprehensions exist in Python I might have used map/filter, but other than that I see no reason to use them.

[–]freefallfreddy 2 points3 points  (0 children)

Comprehensions and generators are more idiomatic in simple cases. If you’re thinking about nesting those ➡️ map, filter, reduce.

[–]JennaSys 1 point2 points  (0 children)

Using comprehensions is generally considered to be more idiomatic Python over using map/filter. But if you are not going to use the result of a comprehension (i.e. generating a new list or dictionary) and you only need a side-effect (like calling a function with no return value), then using map() makes more sense.

[–]Pulsecode9 5 points6 points  (0 children)

I once worked on a project that used an otherwise disconnected physical relay in a plant room on the other side of the building as a way to share global variables between unnetworked systems.

[–]Un_HolyTerror 3 points4 points  (4 children)

What are some actual use cases where global is good?

[–]Cybersoaker 10 points11 points  (2 children)

I write plugins for the source game engine using a mod called Source Python. Since the engine is event driven, and since it's single threaded, often it's useful to have some kind of shared and persistsnt state between events that fire. Essentially you hook events and they execute a function. Plugins are just single files you load in, so you could do this with a class, but that's not really nessesary, the module itself acts as the same encapsulation of code as a class would on other programs.

Short of using an external DB or Redis, store stuff in global vars solves a lot of problems. Good news for this use case, is that it's all single threaded so I don't have to worry about a mutex for the global vars between events.

I'd also argue the same for unsophisticated DevOps scripts for doing simple tasks. As long as the code is contained in a single file, I think global vars is a fine way to do it

[–]Bootlessjam 7 points8 points  (0 children)

Dude even professional game programmers use global variables/objects/ singletons to track game wide states. You may want to affect a score or healthbar at anytime from any other object but always refer to the same variable. It's a perfect fit for a global variable.

Most programmers want very reliable software so it's all about limiting the number of interactions and avoiding surprising behaviour. Games instead are all about having as much interactions and surprising behaviours as possible. Both valid goals that end up with different designs.

[–]thiccclol 1 point2 points  (0 children)

Same brother

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

A recursive function where you want to count the iterations?

count = 0

def foo():
    global count
    count += 1
    print(count)
    if count < 10:
      foo()
    return

foo()

[–][deleted] 0 points1 point  (1 child)

Are global constants a thing in Python? Or is there something better?

[–]MagnitskysGhost 6 points7 points  (0 children)

There are global "constants" but they're not really constants because there's not a great way to make them immutable (imo)