0

I am trying to convert some java code to a python equivalent so that I can interface between the two. So in Java I have some code like this:

public int[] toArray(boolean order) {
    return toArray(order, this.length());
}
public int[] toArray(boolean order, int length) {
    ...
}

and a logical equivalent in python might look like this:

def to_array(self, order):
  return self.to_array(order, self.length())
def to_array(self, order, length):
  ...

except... python doesn't allow function overloading, instead it allows for default parameters, so, again, it would seem logical that you would want to do something like this:

def to_array(self, order, length = self.length()):
  ...

however... this is also not allowed in python, likely since self doesn't really "exist" until within the body of the function.

so what would be the correct way to do this in python? or is it just not possible?

EDIT: I realized I used void functions in my java code that is supposed to return a value so now they return int[]

1 Answers1

4

The pythonic way to do this would be to use None as default value, then test against it:

def to_array(self, order, length=None):
    if length is None:
        length = self.length()
Guillaume
  • 10,463
  • 1
  • 33
  • 47
  • Thanks, that looks like what I am looking for. Seems like python would have a cleaner way of doing this though considering it likes to promote itself as a language that makes it easy to write clean looking code –  Nov 18 '17 at 08:47
  • @JDOdle but as you say *"self doesn't really "exist" until within the body of the function"*, and default parameter values are set at definition time. There can't be an instance when you haven't even finished defining the class! Python prioritises *readability*, and part of that is a consistent conceptual model. – jonrsharpe Nov 18 '17 at 08:58
  • @JDOdle I find this a fairly clean way. It might get more tedious if you wanted to overload a function with 5 different versions, but this is an advantange that compiled languages give you. Each programming language model offers some advantages at the expense of some (perceived) disadvantages. Just like jonsharpe said, it's about what each language considers more important. There's a similar discussion [here](https://stackoverflow.com/a/7456865/2243104) about private methods. At the end of the day, approach your code as the language encourages you to, not what makes sense in another. – Reti43 Nov 18 '17 at 09:04
  • @jonrsharpe that is a fair point, but I don't think that using self in later parameters would create an inconsistent conceptual model. Conceptually, self is just another parameter being passed to the function, it just so happens to be reference to the owner of the function being called. And as evidence of my claim that this is conceptually consistent, I would present C's syntax for declaring array parameters of variable size, which uses a very similar syntax. –  Nov 18 '17 at 09:08
  • @JDOdle I'm not sure how you see a different language with different goals doing something (you don't say exactly what and I'm not that familiar with C) as evidence that it would be consistent *in Python*. Again, default parameter values get bound at definition time, at which point there's no class, no instance, and no value for `self` or any other parameter. If you want to refer to the value of a parameter, you have to do it in the body; *that's* the consistency. – jonrsharpe Nov 18 '17 at 09:16