<br>Check varargs (as another poster mentioned), and consider doing your unit tests in Jython. Some shops that don't want Python for production code are fine with Python for unit tests.<br><br>However, if the reason for preferring java is type checking, you could perhaps get somewhere by suggesting pylint.<br>
<br><div class="gmail_quote">On Fri, Aug 12, 2011 at 10:02 AM, kj <span dir="ltr"><no.email@please.post></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
<br>
<br>
*Please* forgive me for asking a Java question in a Python forum.<br>
My only excuse for this no-no is that a Python forum is more likely<br>
than a Java one to have among its readers those who have had to<br>
deal with the same problems I'm wrestling with.<br>
<br>
Due to my job, I have to port some Python code to Java, and write<br>
tests for the ported code. (Yes, I've considered finding myself<br>
another job, but this is not an option in the immediate future.)<br>
<br>
What's giving me the hardest time is that the original Python code<br>
uses a lot of functions with optional arguments (as is natural to<br>
do in Python).<br>
<br>
As far as I can tell (admittedly I'm no Java expert, and have not<br>
programmed in it since 2001), to implement a Java method with n<br>
optional arguments, one needs at least 2**n method definitions.<br>
Even if all but one of these definitions are simple wrappers that<br>
call the one that does all the work, it's still a lot of code to<br>
wade through, for nothing.<br>
<br>
That's bad enough, but even worse is writing the unit tests for<br>
the resulting mountain of fluffCode. I find myself writing test<br>
classes whose constructors also require 2**n definitions, one for<br>
each form of the function to be tested...<br>
<br>
I ask myself, how does the journeyman Python programmer cope with<br>
such nonsense?<br>
<br>
For the sake of concreteness, consider the following run-of-the-mill<br>
Python function of 3 arguments (the first argument, xs, is expected<br>
to be either a float or a sequence of floats; the second and third<br>
arguments, an int and a float, are optional):<br>
<br>
def quant(xs, nlevels=MAXN, xlim=MAXX):<br>
if not hasattr(xs, '__iter__'):<br>
return spam((xs,), n, xlim)[0]<br>
<br>
if _bad_quant_args(xs, nlevels, xlim):<br>
raise TypeError("invalid arguments")<br>
<br>
retval = []<br>
for x in xs:<br>
# ...<br>
# elaborate acrobatics that set y<br>
# ...<br>
retval.append(y)<br>
<br>
return retval<br>
<br>
My Java implementation of it already requires at least 8 method<br>
definitions, with signatures:<br>
<br>
short[] quant (float[], int , float)<br>
short[] quant (float[], int )<br>
short[] quant (float[], float)<br>
short[] quant (float[] )<br>
<br>
short quant (float , int , float)<br>
short quant (float , int )<br>
short quant (float , float)<br>
short quant (float )<br>
<br>
Actually, for additional reasons, too arcane to go into, I also<br>
need four more:<br>
<br>
short quant (Float , Integer, Float)<br>
short quant (Float , Integer )<br>
short quant (Float , Float)<br>
short quant (Float )<br>
<br>
Writing JUnit tests for these methods is literally driving me<br>
INSANE.<br>
<br>
Some advice on implementing and testing functions with optional<br>
arguments in Java would be appreciated.<br>
<br>
TIA!<br>
<br>
kj<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>