My question is how to use data attributes in a method but allow them to be overridden individually when calling the method. This example demonstrates how I tried to do it:
class Class:
def __init__(self):
self.red = 1
self.blue = 2
self.yellow = 3
def calculate(self, red=self.red, blue=self.blue, yellow=self.yellow):
return red + blue + yellow
C = Class
print C.calculate()
print C.calculate(red=4)
Does it makes sense what I am trying to accomplish? When the calculate function is called, I want it to use the data attributes for red, blue, and yellow by default. But if the method call explicitly specifies a different parameter (red=4), I want it to use that specified value instead. When I run this, it gives an error for using 'self.' in the parameters field (saying it's not defined). Is there a way to make this work? Thanks.
You cannot refer to self since it's not in scope there yet.
The idiomatic way is to do this instead:
def calculate(self, red=None, blue=None, yellow=None):
if red is None:
red = self.red
if blue is None:
blue = self.blue
if yellow is None:
yellow = self.yellow
return red + blue + yellow
"Idiomatic", alas, doesn't always mean "nice, concise and Pythonic".
Edit: this doesn't make it any better, does it...
def calculate(self, red=None, blue=None, yellow=None):
red, blue, yellow = map(
lambda (a, m): m if a is None else a,
zip([red, blue, yellow], [self.red, self.blue, self.yellow]))
return red + blue + yellow
Another option would be to use **kwargs and a class attribute:
class Calc:
defaults = {
'red': 1, 'blue': 2, 'yellow': 3
}
def __init__(self, **kwargs):
self.__dict__.update(self.defaults)
self.__dict__.update(kwargs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With