Take the 2-minute tour ×  Suppose I have a python function that
takes two arguments, but I want the second arg to be optional,
with the default being whatever was passed as the first
argument. So, I want to do something like this:  def
myfunc(arg1, arg2=arg1):     print (arg1, arg2)  Except that
doesn't work. The only workaround I can think of is this:  def
myfunc(arg1, arg2=None):     if arg2 is None:         arg2 =
arg1     print (arg1, arg2)  Is there a better way to do this?
share|improve this question I'm just rethinking this, it might
be possible to achieve with a decorator. It's going to be
unclear what it does at first glance so probably a bad idea, but
it's an idea. –  marcog Jan 1 '11 at 19:39 add comment  3
Answers  up vote 9 down vote accepted  As @Ignacio says, you
can't do this. In your latter example, you might have a
situation where None is a valid value for arg2. If this is the
case, you can use a sentinel value:  sentinel = object() def
myfunc(arg1, arg2=sentinel):     if arg2 is sentinel: