I have been working on a large Python pygame project, and in it I have realized the convenience of calculated properties. I have hit a snag, though; I have a 2 player game where I have a card_group that is shared between the 2 players (it's an online multithreaded game). Whenever I update the card_group, I currently have it as a calculated instance attribute which does x, y, z whenever it is updated. I need that card_group to instead be a CLASS ATTRIBUTE that is shared between the 2 players so that from both players/clients (each on their own connection) they can send/receive/update 1 CLASS ATTRIBUTE instead of 2 P1/P2 INSTANCE ATTRIBUTES. Since the game is very complex and the code has grown quite large (a few thousand lines) I prefer not to have 2 variables when I can simply use 1. It seems more logical to do it this way.
However, I cannot succeed in turning a CLASS ATTRIBUTE into a CALCULATED ATTRIBUTE. I thought it would be Pythonic and simple, but I can't figure out how to do it.
Note: I don't really want to use a method; unless absolutely necessary, I would rather it remain a property/attribute.
I played around with a mock-up version of the code in a test file, but the best I can get it to do is return a property object.
Can I get the value from this property object?
Is it possible to use the @property decorator or the property() method to make a CLASS ATTRIBUTE work just like a CALCULATED INSTANCE ATTRIBUTE?
Is there another way to make a calculated class attribute?
class Game: _card_group = 0 @property def card_group(self): return self._card_group @card_group.setter def card_group(self, val): print('Setter') self._card_group = val Game.card_group = 3 print(Game.card_group)
In the example above, when Game.card_group = 3 is executed, it does not call the card_group.setter as I expected, and whenever I print(Game.card_group), it prints out a property object.
Thanks in advance!