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

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@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

-1 Please, send to Ginger/Gingerbase list Rodrigo On 06/07/2016 01:30 AM, Ramon Medeiros wrote:
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@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)

It was not applied! Only the Kimchi patch was applied. On 06/08/2016 01:01 AM, Aline Manera wrote:
Applied. Thanks.
Regards,
Aline Manera
_______________________________________________ Kimchi-devel mailing list Kimchi-devel@ovirt.org http://lists.ovirt.org/mailman/listinfo/kimchi-devel

On 06/08/2016 01:02 AM, Aline Manera wrote:
It was not applied!
Only the Kimchi patch was applied.
HAHAHAHHAHAHAHAHA
On 06/08/2016 01:01 AM, Aline Manera wrote:
Applied. Thanks.
Regards,
Aline Manera
_______________________________________________ Kimchi-devel mailing list Kimchi-devel@ovirt.org http://lists.ovirt.org/mailman/listinfo/kimchi-devel
_______________________________________________ Kimchi-devel mailing list Kimchi-devel@ovirt.org http://lists.ovirt.org/mailman/listinfo/kimchi-devel
participants (4)
-
Aline Manera
-
Daniel Henrique Barboza
-
Ramon Medeiros
-
Rodrigo Trujillo