Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to override data attributes in method calls?

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.

like image 663
Elliott Avatar asked Jul 01 '26 23:07

Elliott


2 Answers

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
like image 162
Thomas Avatar answered Jul 04 '26 11:07

Thomas


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)
like image 25
Richard Levasseur Avatar answered Jul 04 '26 11:07

Richard Levasseur