Message212876
If you have a descriptor (in my case it was an SQLAlchemy column) on an instance and this descriptor returns None for a call to __get__ then getattr with a given default value, will not return the default, but None.
I have no knowledge on the implementation details of getattr but I guess the logic is something like this:
- getattr looks at the given object and sees that the attribute in question is not None (since it is the descriptor object)
- getattr returns the descriptor
- the descriptors __get__ method is called
- __get__ return None
Maybe it should be more like this:
- getattr looks at the given object and sees that the attribute in question is not None (since it is the descriptor object)
- getattr sees that the attribute has __get__
- getattr calls __get__ method and looks if the return value is None
I'm not sure if this is really a bug but it's highly confusing and somewhat un-pythonic. I really should not care of an attribute of an object is a value or a descriptor. This is especially true since this problem also applies to @property. Effectively this means that if you call getattr you have *know* if the name in question is a property or not and one can't simply swap out an objects value for a property without risking to break calling code.
If this is actually *not* a bug, we should at least update the documentation to getattr, to mention this fact. Because currently it states that "getattr(x, 'foobar') is equivalent to x.foobar" which is obviously not true. |
|
Date |
User |
Action |
Args |
2014-03-07 12:46:12 | Martin.Thurau | set | recipients:
+ Martin.Thurau |
2014-03-07 12:46:12 | Martin.Thurau | set | messageid: <1394196372.69.0.347625630871.issue20864@psf.upfronthosting.co.za> |
2014-03-07 12:46:12 | Martin.Thurau | link | issue20864 messages |
2014-03-07 12:46:12 | Martin.Thurau | create | |
|