Chris
Chris

Reputation: 31206

Method overloading in Python: more overloading

this question is not a duplicate of the other overloading question because I am attempting to reference self.args in the function. The answer given in that question does not satisfy, since if I implement that answer, I will get an error. However, in other languages, this is easily done through method overloading...so this question is highly related to the other question.

So I have a method,

class learner():
      def train(a ton of arguments):
            self.argA = argA, 
            etc.

And I want to call train with just one value, and have it use all the self calls to populate the other arguments...but it is a circular reference that python doesn't seem to support. Ideally, I would write:

class learner():
    def train(self, position, data = self.data, alpha = self.alpha, beta = etc):
          ...do a bunch of stuff

    def roll_forward(self,position):
          self.alpha += 1
          self.beta += 1
          self.train(position)

How would I do this? In other languages, I could just define a second train function that accessed the internal variables...

currently, I have a janky hack where I do this:

class learner():
     def train(...):
             ....
     def train_as_is(self,position):
         self.train(position,self.alpha,self.beta, etc.)

But this is a pretty big class, and a ton of functions like that are turning my code into spaghetti...

Upvotes: 1

Views: 105

Answers (3)

Daniel Roseman
Daniel Roseman

Reputation: 599580

An enhancement on other answers is to use a defaults dictionary:

def train(self, position, **kwargs):
    defaults = {
        'a': self.a,
        'b': self.b,
         ...etc...
    }
    defaults.update(kwargs)
    ... do something with defaults

def roll_forward(self,position):
      self.alpha += 1
      self.beta += 1
      self.train(position)

Upvotes: 6

Copperfield
Copperfield

Reputation: 8510

you can give those parameter a default value of None, then check if any of them is None and if it is, assign the desired value like this

class learner():
    def train(self, position, data = None, alpha = None, beta = None,etc...):
        if data is None:
            data = self.data
        if alpha is None:
            alpha = self.alpha
        ...etc
        ...do a bunch of stuff

Upvotes: 1

miki725
miki725

Reputation: 27861

Not sure if I follow your question 100% but usual pattern is to use None as a default value:

class Foo(object):
    def __init__(self):
        self.a = ...
        self.b = ...
        ...

    def foo(self, position, a=None, b=None, ...):
        if a is None:
            a = self.a
        if b is None:
            b = self.b
        ...

You can simplify that by using or:

def foo(self, position, a=None, b=None, ...):
    a = a or self.a
    b = b or self.b
    ...

however that is not as reliable in case you will try to pass falsy values like 0 the or part will kick in

Upvotes: 4

Related Questions