Default method arguments

Nicola Larosa nico-NoSp at m-tekNico.net
Tue Nov 15 11:17:20 EST 2005


> I have little problem:
> 
> class A:
>     def __init__(self, n):
>         self.data = n
>     def f(self, x = ????)
>         print x
> 
> All I want is to make self.data the default argument for self.f(). (I
> want to use 'A' class as following :
> 
> myA = A(5)
> myA.f()
> 
> and get printed '5' as a result.)

# use new-style classes, if there's no cogent reason to do otherwise
class A(object):
    def __init__(self, n):
        self.data = n
    def f(self, x = None)
        # do NOT use "if not x" !
        if x is None:
            print self.data
        else:
            print x

-- 
Nicola Larosa - nico-NoSp at m-tekNico.net

...Linux security has been better than many rivals. However, even
the best systems today are totally inadequate. Saying Linux is
more secure than Windows isn't really addressing the bigger issue
 - neither is good enough. -- Alan Cox, September 2005



More information about the Python-list mailing list