From c7dce2adecc7f8e2c2d2fae00a5cb412ed2a3f94 Mon Sep 17 00:00:00 2001 From: Nikola Dipanov Date: Mon, 2 Jun 2014 18:24:32 +0200 Subject: [PATCH] Initialize objects field in ObjectsListBase class Normally the underlying value of an object attribute is set upon first attempt to set it and would not exist otherwise. This causes the object attribute access mechanism to fall back to obj_load_attr. This is mostly fine for a lot of use cases, but it also means that for ObjectListBase subclasses, it is not possible to construct them empty and then call list methods on them without resorting to something like: myListObject(objects=[]) This patch makes ObjectListBase default it's objects filed to [] if it's not passed in allowing us to have a cleaner API and call list methods such as len() on objects constructed on the fly.. Change-Id: Iad1301cc4f88018f10f3dd6c76e35275cd0c51f6 --- nova/objects/base.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nova/objects/base.py b/nova/objects/base.py index 2f6c649..e5b6349 100644 --- a/nova/objects/base.py +++ b/nova/objects/base.py @@ -505,6 +505,12 @@ class ObjectListBase(object): # requested of the list object. child_versions = {} + def __init__(self, *args, **kwargs): + super(ObjectListBase, self).__init__(*args, **kwargs) + if 'objects' not in kwargs: + self.objects = [] + self._changed_fields.discard('objects') + def __iter__(self): """List iterator interface.""" return iter(self.objects)