for attribute in ['x', 'y', 'parent', 'force', 'hunger', 'size']:
row.append(getattr(actor, attribute, 'none'))
getattr
takes an optional 3rd argument setting a default value for if the object doesn't have the attribute.
Some notes here:
Why are some of these actors missing attributes? Should they instead have 'none'
or None
or some other default value?
Lists of strings are prone to bugs, due to implicit string literal concatenation. This is one of many good reasons to get a linting tool, if you're not using one already.
Are you sure you want 'none'
as a default? It might make more sense to use None
, or a zero-like value of whatever type the attributes are expected to be. (If they're expected to be strings, 'none'
may make sense.)