[libvirt] [PATCH 11/22] WmiClass: Don't share "versions" between instances

Radostin Stoyanov posted 22 patches 7 years, 3 months ago
[libvirt] [PATCH 11/22] WmiClass: Don't share "versions" between instances
Posted by Radostin Stoyanov 7 years, 3 months ago
Lists in Python are mutable and when used as a default value of a
parameter for class constructor, its value will be shared between
all class instances.

Example:

	class Test:
	    def __init__(self, mylist=[]):
	        self.mylist = mylist

	A = Test()
	B = Test()
	A.mylist.append("mylist from instance A")
	print(B.mylist) # Will print ['mylist from instance A']

Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
---
 src/hyperv/hyperv_wmi_generator.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/hyperv/hyperv_wmi_generator.py b/src/hyperv/hyperv_wmi_generator.py
index f98a77562..acb9e5440 100755
--- a/src/hyperv/hyperv_wmi_generator.py
+++ b/src/hyperv/hyperv_wmi_generator.py
@@ -44,9 +44,9 @@ class WmiClass:
     to.
     """
 
-    def __init__(self, name, versions = []):
+    def __init__(self, name, versions=None):
         self.name = name
-        self.versions = versions
+        self.versions = versions if versions else list()
         self.common = None
 
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 11/22] WmiClass: Don't share "versions" between instances
Posted by Daniel P. Berrangé 7 years, 3 months ago
On Sat, Mar 17, 2018 at 02:23:29PM +0000, Radostin Stoyanov wrote:
> Lists in Python are mutable and when used as a default value of a
> parameter for class constructor, its value will be shared between
> all class instances.
> 
> Example:
> 
> 	class Test:
> 	    def __init__(self, mylist=[]):
> 	        self.mylist = mylist
> 
> 	A = Test()
> 	B = Test()
> 	A.mylist.append("mylist from instance A")
> 	print(B.mylist) # Will print ['mylist from instance A']
> 
> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
> ---
>  src/hyperv/hyperv_wmi_generator.py | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)


Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list