Re: Keeping track of state without globals?

@13 I am not sure if that actually improves the situation over using global variables, since all you are doing is wrapping then in a class definition, so it is still global state, albeit wrapped in a class. as a general rule, it isn't a great idea to have class-level fields that are meant to be mutated. best for those to be read-only information, since then it leads to the same problems as with global mutable state. the usefulness of a class is in acting as a template for objects. if all the fields of a class are at the class-level, then it is all just static and doesn't have nmuch of a point, since every instance of that class will be the same...

btw, I couldn't open your code example and from your description it isn't fully clear what the problem is, so I can't give very concrete advice. how to deal with state in a program largely depends on the program itself, particularly if you arre using some particular library for handling events. maybe for your program it might not even be a huge deal that you use a global variable  to hold on to state. usually global variables cause problems in making code harder to test or contention between different parts of the code that use the same state. good to know what problem you are trying to solve, so one doesn't end up following some coding guideline just for the sake of folowing it.

one thing you wrote:
"... If I return the tuple, then pass it back into the function that creates it in the first place, it'll just get overwritten ..."

not sure what you mean by this. maybe you could give a concrete example of what you mean, but you can certainly take a function that takes a tuple and returns a new tuple and run the function in a loop iteratively passing it the new result. not sure if that is what you meant. example below

def func(n):
  print(n)
  return n + 1

def prog():
  state = 0
  while True:
    state = func(state)
    if state >= 10:
      break

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Dragonlee via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Dragonlee via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : GrannyCheeseWheel via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : GrannyCheeseWheel via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : GrannyCheeseWheel via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Dragonlee via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Dragonlee via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : GrannyCheeseWheel via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : GrannyCheeseWheel via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Dragonlee via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : GrannyCheeseWheel via Audiogames-reflector

Reply via email to