is there any way to reference a class name from within the class declaration? an example follows:
class Plan(SiloBase):
cost = DataField(int)
start =
One can do this with the following code in python3. We use cached_property to only evaluate the Player.enemyPlayer once and then return the cached result. Because our value comes from a function, it is not evaluated when the class is first loaded.
class cached_property(object):
# this caches the result of the function call for fn with no inputs
# use this as a decorator on function methods that you want converted
# into cached properties
def __init__(self, fn):
self._fn = fn
def __set_name__(self, owner, name):
# only works in python >= 3.6
self.name = name
self._cache_key = "_" + self.name
def __get__(self, instance, cls=None):
if self._cache_key in vars(self):
return vars(self)[self._cache_key]
else:
result = self._fn()
setattr(self, self._cache_key, result)
return result
class Player:
@cached_property
def enemyPlayer():
return Player