Default method arguments

bruno at modulix onurb at xiludom.gro
Tue Nov 15 18:44:23 CET 2005


Dennis Lee Bieber wrote:
> 
> 
(snip)
> but that may not be desirable if None is a valid value => myA.f(None),
> so...
> 
> class A(object):
>     def __init__(self, n):
>         self.data =n
>     def f(self, *arg):
>         if len(arg) == 0:
>             x = self.data
>         else:
>             x = arg[0]
>         print x

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


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list