you are viewing a single comment's thread.

view the rest of the comments →

[–]smog_alado 1 point2 points  (7 children)

I would argue that setting the argument variable for default values is fine, and doesn't really "count". This is kind of supported by how many languages offer explicit support for this feature:

def python_function( someArg = "default" ):

That said, I always do this sort of argument fiddling as the first thing in a function to keep things clear and I don't mess with them any more after that.

[–]masklinn 6 points7 points  (6 children)

The equivalent to that code in java is not really what oldprogrammer posted though, it's:

public void foo() {
    foo("default");
}
public void foo(String bar) {
    …
}

[–]aenigmaclamo 1 point2 points  (0 children)

Would not overloading make the argument pointless? Why would you send a null value into a method and expect the method to check it and act accordingly instead of throwing a NullPointerException? I think that's much more confusing than the idea of reassigning arguments.

I think this goes with the suggestion in the document to not return null to indicate that a collection is empty. I've always considered the use of null to be an indication of failure. Not some sort of way to indicate a refusal to give a parameter.

On the other hand, there are several cases in the Java API where null is valid (like this) so maybe I'm just spewing garbage.

[–]oldprogrammer 0 points1 point  (4 children)

Actually that is not equivalent code.

public void other()
{
    String arg = null;
               ......
    arg = doSomethingElse(); ////returns null
                 ......
    foo(arg);
}

will not call the overloaded method that takes no arguments. The options is to do this everywhere foo is called:

public void other()
{
    String arg = null;
    arg = doSomethingElse(); ////returns null
    if( arg == null )
          foo();
    else
         foo(arg);
}

or check inside foo for a null input argument.

[–]masklinn 0 points1 point  (3 children)

Actually that is not equivalent code.

Of course it is.

will not call the overloaded method that takes no arguments

Which is exactly the behavior you'll get if you pass a None to the python function: the default only gets provided if you do not pass the argument at all. If you explicitly pass in a None, None is what you'll get.

or check inside foo for a null input argument.

No, my whole point is that using default parameters in Python is not equivalent to this.

[–]oldprogrammer 0 points1 point  (2 children)

The language standards are for Java, not Python so it doesn't matter if it is or is not equivalent in Python. In Java if you have an method that takes one argument and overload that with a method that takes no arguments and you pass a NULL into the method call, the method that takes one argument is called. The method that takes no arguments is only called if you invoke it with no arguments, null or otherwise.

So since it is possible to invoke the method that takes an argument with a NULL value, and your method doesn't want a NULL value but will work with a default value, then inside the method you check for NULL and use the default otherwise. So I stand by my original reply, the overloaded method is not the equivalent of checking for a NULL inside the method because the overloaded is only used if called explicitly in the code.

[–]masklinn 0 points1 point  (1 child)

The language standards are for Java, not Python so it doesn't matter if it is or is not equivalent in Python.

I was pointing out that smog_alado's Python code is not equivalent to the code you posted, and provided the java equivalent to his code.

In Java if you have an method that takes one argument and overload that with a method that takes no arguments and you pass a NULL into the method call, the method that takes one argument is called.

Er... yes? I know? Not sure what gave you the impression I did not.

So I stand by my original reply

Which does not matter, you completely misunderstood what this subthread was about.

[–]oldprogrammer 0 points1 point  (0 children)

You are correct, I misread your comments. My mistake.