So I'm programming a game and I need a datatype that can store a multitude of variables ranging from lists
, tuples
, strings
and integers
. I'm torn between using either dictionaries
or namedtuples
.
GameData = namedtuple('GameData', ['Stats', 'Inventory', 'Name', 'Health'])
current_game = GameData((20,10,55,3), ['Sword', 'Apple', 'Potion'], 'Arthur', 100)
GameData = {'Stats': (20,10,55,3), 'Inventory': ['Sword', 'Apple', 'Potion'], 'Name': 'Arthur', 'Health': 100}
You see, the biggest problem here is all of these values may change, so I need a mutable datatype, which is not the namedtuple
. Looking in the docs, namedtuples
appear to have ._replace()
, so does that make it mutable?
I also like how namedtuples
have a __repr__
method that prints in the name=value
format. Also, the functionality of assigning separate __doc__
fields to each value in the namedtuple
is very useful. Is there this functionality with dictionaries
?