Default method arguments

Mike Meyer mwm at mired.org
Tue Nov 15 15:39:27 EST 2005


Benji York <benji at benjiyork.com> writes:
> I'll add my 2 cents to the mix:
>
> default = object()
>
> class A(object):
>      def __init__(self, n):
>          self.data = n
>
>      def f(self, x=default):
>          if x is default:
>              x = self.data
>          print x

There were a lot of solutions like this. I'd like to point out that
you can put the "marker" in the class:

class A(object):
   default = object()
   def __init__(self, n):
       self.data = n

   def f(self, x = default):
       if x is self.default:
          x = self.data
       print x

This way you don't pollute the module namespace with class-specific
names. You pollute the class namespace instead - which seems like an
improvement.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list