The correct way to do this is:

def mything(a, b, c=None):
 """ do something, like check if 'c' is not None """
 if c:
  print a, b, c
  print a, b

then when you call mything with either 2 or 3 arguments the runtime figures out if 'c' should be passed, if not, it uses the default value, in this case None.

mything(1, 2)
 - and -
mything(1, 2, 3)

will both work.


-----Original Message-----
[]On Behalf Of Tom
Sent: Wednesday, April 04, 2001 12:22 PM
Subject: [Jython-users] overloading methods

Please pardon my basic "newbie" question...I've not been able to find the
answer to this in the tutorial, in O'Reilly, or thru web searching.

I am making a .py file with a bunch of methods.  I'd like to overload (at
least) one of them, like:

def mything(a,b,c)
  print a,b,c
def mything(a,b)
  print a,b

What I've found is that only the last one in the .py file seems to be called
every time.  If I try to reference the above with:


I get an error message like: "not enough arguments; expected 3 got 2".

I've overcome this by using a single method like:

def mything(*a)

and looking at the len(a).  Since Jython does such a great job when trying to
figure out which overloaded method to use in a Java class, I thought I must be
missing something. Is there a better/preferred way to accomplish this?

Thanks ahead for your advice & guidance,

Tom Whittaker (
University of Wisconsin-Madison
Space Science and Engineering Center
Phone/VoiceMail: 608/262-2759
Fax: 608/262-5974

Jython-users mailing list