[Kimchi-devel] [PATCH][Gingerbase] Do not use default value when declare a function

Ramon Medeiros ramonn at linux.vnet.ibm.com
Tue Jun 7 04:30:12 UTC 2016


For mutable object as a default parameter in function and
method-declarations the problem is, that the evaluation and creation
takes place at exactly the same moment. The python-parser reads the
function-head and evaluates it at the same moment.

Most beginers assume that a new object is created at every call, but
that's not correct. One object (in your example a list) is created at
the moment of declaration and not on demand when you are calling the
method.

For imutable objects that's not a problem, because even if all calls
share the same object, it's imutable and therefore it's properties
remain the same.

As a convention, must be used the None object for defaults to indicate
the use of a default initialization, which now can take place in the
function-body, which naturally is evaluated at call-time.

Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
---
 disks.py                | 4 +++-
 model/packagesupdate.py | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/disks.py b/disks.py
index 2883431..a1ba7c2 100644
--- a/disks.py
+++ b/disks.py
@@ -47,7 +47,9 @@ def _get_dev_node_path(maj_min):
     return "/dev/%s" % data["DEVNAME"]
 
 
-def _get_lsblk_devs(keys, devs=[]):
+def _get_lsblk_devs(keys, devs=None):
+    if devs is None:
+        devs = []
     out, err, returncode = run_command(
         ["lsblk", "-Pbo"] + [','.join(keys)] + devs
     )
diff --git a/model/packagesupdate.py b/model/packagesupdate.py
index 073667c..42011f7 100644
--- a/model/packagesupdate.py
+++ b/model/packagesupdate.py
@@ -56,11 +56,13 @@ class PackageUpdateModel(object):
 
         return self.host_swupdate.getUpdate(name)
 
-    def _resolve_dependencies(self, package=None, dep_list=[]):
+    def _resolve_dependencies(self, package=None, dep_list=None):
         """
         Resolve the dependencies for a given package from the dictionary of
         eligible packages to be upgraded.
         """
+        if dep_list is None:
+            dep_list = []
         if package is None:
             return []
         dep_list.append(package)
-- 
2.5.5




More information about the Kimchi-devel mailing list