Re: Keeping track of state without globals?

global mutable state sure is something you want to avoid, since it can be accessed anywhere, making it difficult to reason about. one way to limit the scope definitely is what has been proposed, to use a class and make the mutable state a field of the class. that way only the methods should be the ones mutating that state, but I would actually agree with OP that OOP often leads to over engineered code and can actually get really messy. not saying OOP is totally bad, but it is also entirely unnecessary. yes it is necessary to use classes in python to create convenient packages of various variables grouped together, however it is not necessary to do the Oop thing of mixing data and logic by giving those classes methods that are responsible for operating on the fields of the class.

I can propose a simple procedural style, data-oriented

design where data and logic are seprated, which imo is simpler and cleaner.

Again, not saying this is the good way to do it and the Oop way is the bad way. The OOP way is fine too, but just want to provide an alternative, as this thread is giving the impression that it is the only way.

So how else can we limit scope of mutable state? Well simplest way is instead of global variables to have local variables, either in a loop or a function. Let’s say I have a main function that gets called when the program starts up. You can just initialize all your state as local variables and when you need some function to have access to it, you just explicitly pass it as a argument to that function. That function can then read that state you pass it and if it is mutable  then it can also change it. Small example below:

def main():
  state = True # in this case state is just a boolean
  while True:
    foo(state) # foo will be able to read state
    # since booleans like all primitives are immutable, in order for a function to change it
    # it will have to return a new value and the caller will have to update the state
    new_state = bar(state)
    state = new_state
  # end while
#end def

If you want to package related data together so it is more convenient to pass around and also gives you a mutable container then the simplest and most efficient is to use pythons relatively new dataclasses.

from dataclasses import dataclass

@dataclass
class InventoryItem:
    """Class for keeping track of an item in inventory."""
    name: str
    unit_price: float
    quantity_on_hand: int = 0

this neat decorator will auto generate an __init__ and __repr__ function, so you can use it simply like this:
item = InventoryItem("chocolate",  2.5)
item.unit_price #access a field
item.quantity_on_hand  = 50 # mutate the state

since the dataclass acts as a mutable container, you don't have to do as was done in my previous example of having the function return a new state and the caller being responsible for updating it. although that way has major advantages in being able to reason about the code and making your functions testable. instead when passing a dataclass, the function can mutate it directly:

def sell_item(item: InventoryItem):
  item.quantity_on_hand -= 1 # change is reflected everywhere

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/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 : GrannyCheeseWheel 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 : GrannyCheeseWheel via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn 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 : 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

Reply via email to