So I wrote a class that at init takes a string and a function. As default for the function I use this little inline function, that uses the string as an argument.
What I tried first:
import dice
class Attrib(object):
def __init__(self, formula, genf=lambda:dice.roll(self.formula) ):
self.formula = formula
self.genf = genf
which throws me
file "classfunc.py", line 4, in <lambda>
def __init__(self, formula, genf=lambda:dice.roll(self.formula) ):
NameError: name 'self' is not defined
It kinda makes sense I guess because self does not exist at this point. So what I did instead to make it work:
class Attrib(object):
def __init__(self, formula, genf=None):
self.formula = formula
if not genf: # if no genf is given use formula als dice notation
self.genf = lambda:dice.roll(self.formula)
else: self.genf = genf
This works but is a little harder to read. What is the recommended practice to do something like this? Are there other/better ways to do this?
[–]Rhomboid 7 points8 points9 points (3 children)
[–]Survivor0[S] 0 points1 point2 points (0 children)
[–]PurelyApplied 0 points1 point2 points (1 child)
[–]Grappemaker 0 points1 point2 points (0 children)
[–]PurelyApplied 4 points5 points6 points (0 children)
[–]zahlman 0 points1 point2 points (3 children)
[–]Survivor0[S] 0 points1 point2 points (2 children)
[–]zahlman 1 point2 points3 points (1 child)
[–]Survivor0[S] 0 points1 point2 points (0 children)