vampire-public
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Vampire-public] Default variables in Python


From: Nicolas Burrus
Subject: [Vampire-public] Default variables in Python
Date: Fri, 9 Jan 2004 16:31:57 +0100
User-agent: KMail/1.5.4

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I've discovered a weird problem with default arguments which are mutable 
(lists, dictionaries).

One should not write:

def foo(arg1 = []):
    self.arg1 = arg1
    # ...

because arg1 default value will be shared.  That is, if foo modifies it, the 
next time foo will be called, the default argument will not be an empty list.

Instead, I use the following code:

def foo(arg1 = None):
    self.arg1 = arg1 or {}

The problem is explained here: http://www.python.org/doc/faq/general.html#id51

I've removed problematic code in Vampire, it should come in next commits.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE//sluBI1+iQ4q66sRAn39AJ4hM+P/hMRfCQEsQ2v3vnRWVaMoagCfafRN
9y5vYIUMY3Rwi34iQ1Cd1Mk=
=JN9w
-----END PGP SIGNATURE-----




reply via email to

[Prev in Thread] Current Thread [Next in Thread]