Default method arguments

Bill Mill bill.mill at gmail.com
Tue Nov 15 11:16:51 EST 2005


On 15 Nov 2005 08:03:26 -0800, gregory.petrosyan at gmail.com
<gregory.petrosyan at gmail.com> wrote:
> Hello everybody!
> 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.)
>

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

    def f(self, x=None):
        if not x:
            x = self.data
        print x

>>> myA = A(5)
>>> myA.f()
5

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list