I saw the page:
I'm familiar with this SO question for creating enums in Python. However, I can't find an example anywhere of an enum that has functions.
I'm mainly a Java programmer. I wrote this code in Java:
public enum Role {
SOLDIER(4),
DEMOMAN(2),
SCOUT(4),
MEDIC(2);
private final int maxPlayers;
private Role(int maxPlayers) {
this.maxPlayers = maxPlayers;
}
public int getMaxPlayers() { return maxPlayers; }
}
I tried to do the same in Python:
class Klass:
SCOUT = 1
SOLDIER = 2
DEMOMAN = 3
MEDIC = 4
@staticmethod
def maxPlayers(klass):
return {
Klass.SCOUT : 4,
Klass.SOLDIER : 4,
Klass.DEMOMAN : 2,
Klass.MEDIC : 2,
}[klass]
For some reason. I feel like I'm doing it wrong. What is the best practice for associating functions to enums in Python?
I don't actually care if the suggested answer doesn't use an enum; I'm just trying to understand the best practice for implementing the above Java code in Python.
I'm willing to use this enum in the following:
class Players(dict):
def countKlass(self, klass):
count = len(filter(lambda x: x == klass, self.values()))
return count