milesrichardson / ParsePy

A relatively up-to-date fork of ParsePy, the Python wrapper for the Parse.com API. Originally maintained by @dgrtwo
MIT License
515 stars 184 forks source link

Should undfined fields return None rather than throwing an exception? #114

Closed flound1129 closed 9 years ago

flound1129 commented 9 years ago

When I code with ParsePy, my code is invariably strewn with these:

try:
    value = item.field
except:
    value = ""

Shouldn't the library just return None if the field is undefined, rather than throwing an exception?

dankrause commented 9 years ago

If we returned None for an undefined field, there would be no way to tell whether or not a field existed, but contained the value None.

flound1129 commented 9 years ago

Couldn't you throw an exception if the field didn't exist, and return None if the field existed and had no value?

flound1129 commented 9 years ago

For comparison, the (official) JS Api query.get("field") returns undefined whether the field exists or not.

lullis commented 9 years ago

Bear in mind that Javascript and Python are very different in how they define types and falseness. What you are trying to do can work in Javascript because undefined is falsy, just like null. In Python, None is falsy, a non existent attribute is an exception.

But fear not: you can always use python's getattr. value = getattr(item, 'field', None) will do exactly what you want.