views:

71

answers:

3

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.

+4  A: 

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
Thomas
As you said, not as concise as one would like -- but certainly serviceable. Thanks much!
Elliott
Then hit the green check mark ;)
Thomas
A: 

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
gruszczy
If the colours can be 0 (which it likely can), the first approach fails. The second is okay.
Thomas
That's true. Thanks for pointing it out :-)
gruszczy
+1  A: 

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)
Richard Levasseur