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
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user