[Pulp-list] Default values in method signatures
Jay Dobies
jason.dobies at redhat.com
Thu Mar 10 19:19:49 UTC 2011
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 03/10/2011 02:08 PM, Jay Dobies wrote:
>
> This came up today with respect to the Filter __init__ method. The issue
> surrounds the signature: def __init__(self, id, type, description=None,
> package_list=[]):
>
> I first learned this was an issue when PyCharm started highlighting them
> in our code base. From PyCharm's inspection description:
>
> "Default argument values are evaluated only once at function definition
> time, which means that modifying the default value of the argument will
> affect all subsequent calls of the function. "
>
> In other words, all Filter instances that don't pass in package_list
> will point to the same list object. If they start to dork with its
> contents, *all* Filter objects will reflect the package list.
>
> A workaround could be:
>
> def __init__(self, id, type, description=None, package_list=None):
> if not package_list:
> package_list = []
> self.package_list = package_list
>
> That way a new list is created on each instantiation if one is not
> passed in.
>
> BTW, this is all over our code. I mentioned it back in August
> (https://www.redhat.com/archives/pulp-list/2010-August/msg00030.html) so
> please try to clean these up when you see them.
One quick note I forgot to mention, this applies to {} created in method
signatures as well.
_______________________________________________
Pulp-list mailing list
Pulp-list at redhat.com
https://www.redhat.com/mailman/listinfo/pulp-list
- --
Jay Dobies
RHCE# 805008743336126
Freenode: jdob
http://pulpproject.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJNeSRVAAoJEOMmcTqOSQHC8ZcH/2scUN4uHdgbyVtSmPyWoSf9
keRq2FM41/0Fvox0Lruiu/j4ni3fDDVEAkWKkrF29cHdosmsHJ6nRMzFTcLSYY/N
uhNd7DrlC2/KG2ZzAO7xGOZZBOgX3UvCQMo7sUgwg5Pxpe8XQckCWFFbQFCTzS12
O9FiEmrcTaoSyoW+cJgMSsKFrZYrhP9N7B37M5Jw4ft2nZSccHsezTH4Wsf65PgJ
g56PzjZbT9pxjDNWUX4VkNXsHtlGGpw1R3t6zMyL4uobrXIu26JnJRPYMhvAtn1s
zVqj+GwkFAJi3LQZJyg6M8XkOrBCukVl5iEkUCRuaexWXqKFYMuf15aWdIgedDM=
=iwRa
-----END PGP SIGNATURE-----
More information about the Pulp-list
mailing list