[Erp5-dev] Default parameter (list) [eg] in OrderBuilder - which way is better?
Mikolaj Antoszkiewicz
mikolaj at erp5.pl
Thu Jun 12 10:54:18 CEST 2008
Łukasz Nowak wrote:
> Hello,
>
> If list is passed as optional argument, its defaults shall be None or
> []?
>
> Take for example ERP5/Document/OrderBuilder.py - everywhere foo_list on
> method argument list has defaults to None. Without proper initialisation
> there are problems, if method is called without changing parameter to
> empty list eg.
>
> Which is more correct:
>
> def funA(self, movement_list=None):
> if movement_list is None:
> movement_list = []
> =snip=
>
> def funB(self, movement_list=[]):
> =snip=
>
> ?
>
In general, it's not desirable to use unmutables as default parameters.
They behave differently than usually expected to (consequent calls to
that function will work on existing list, not on a new empty one as one
might expect)
That's why funB type should be used only if You know what You're doing
and You want that.
Mikolaj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3229 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.tiolive.com/pipermail/erp5-dev/attachments/20080612/f6a9017e/attachment.bin>
More information about the Erp5-dev
mailing list