python - How do I reliably check if an attribute is defined for an object? -
sometimes create objects x
attribute defined, don't.
class object(models.model): x = models.integerfield(blank=true, null=true)
i want create subset of objects x
not defined @ point in app:
[o o in os if not hasattr(o,'x')]
i've being playing around in shell, , don't results want.
>>> o = object() >>> o.save() >>> o.id 1l >>> hasattr(o,'x') true
is checking x == none
sufficient?
yes, checking x none
sufficient.
any instance of object
have attribute x
, regardless of whether set - it'll none
if didn't set it.
Comments
Post a Comment