Reputation: 1371
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.
Upvotes: 3
Views: 1116
Reputation: 14882
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)
Upvotes: 1
Reputation: 181745
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
Upvotes: 4
Reputation: 42168
You can write it in less lines:
def calculate(self, red=None, blue=None, yellow=None):
red = self.red if red is None else red
blue = self.blue if blue is None else blue
yellow = self.yellow if yellow is None else yellow
return red + blue + yellow
Upvotes: 0