Default method arguments

Benji York benji at benjiyork.com
Tue Nov 15 14:41:50 EST 2005


bruno at modulix wrote:
 > Another solution to this is the use of a 'marker' object and identity 
test:
 >
 > _marker = []
 > class A(object):
 >     def __init__(self, n):
 >         self.data =n
 >     def f(self, x = _marker):
 >         if x is _marker:
 >             x = self.data
 >         print x

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
--
Benji York



More information about the Python-list mailing list