About __init__ and default arguments

Scott David Daniels Scott.Daniels at Acm.Org
Sat Apr 12 14:18:54 CEST 2008


Nathan Duran wrote:
> 
> On Apr 11, 2008, at 11:35 AM, python-list-request at python.org wrote:
>> I'd like to assign the value of an attribute in __init__ as the default
>> value of an argument in a method.  See below:
> Why not just do
> 
> def franklin(self, keyword):
>     if not keyword: keyword = self.default
>     return "A %s in time saves nine." % (keyword)

Several things are false (for example: '', 0, False, [], ...)
If you can get along with the code you have suggested, I'd
think about using:

def franklin(self, keyword):
     return "A %s in time saves nine." % (keyword or self.default)

-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list