Default method arguments

bonono at bonono at
Wed Nov 16 10:27:12 CET 2005

What you want is essentially :

if parm_x is not supplied, use self.val_x

So why not just express it clearly at the very beginning of the
function :

def f(self, parm_x=NotSupplied, parm_y=NotSupplied ,,,)
  if parm_x is NotSupplied: parm_x = self.val_x
  if parm_y is NotSupplied: parm_y = self.val_y

Much easier to understand than the "twisting your arm 720 degree in the
back" factory method, IMO.

Gregory Petrosyan wrote:
> Thanks a lot, but that's not what I do really want.
> 1) f() may have many arguments, not one
> 2) I don't whant only to _print_ x. I want to do many work with it, so
> if I could simply write
> def f(self, x =                           (*)
> it would be much better.
> By the way, using
> class A(object):
>     data = 0
>     ....
>     def f(self, x = data)
> solves this problem, but not nice at all
> So I think (*) is the best variant, but it doesn't work :(

More information about the Python-list mailing list