On Apr 1, 2016 17:07, "Volker Braun" <vbraun.n...@gmail.com> wrote:
>
> On Friday, April 1, 2016 at 4:19:08 PM UTC+2, Erik Bray wrote:
>>
>> One of my top wishlist items for Python 4 is some kind of __hasattr__
>> special method for classes and/or an equivalent for descriptors that
>> merely guarantees* that accessing the attribute of that name will not
>> result in an AttributeError.  That would be extremely useful for
>> checking attribute availability without computing / returning a value
>
>
> IMHO its generally bad to raise exceptions from @properties. Besides the
obvious usability issue (why does tab completion raise a ZeroDivision
error?), properties shouldn't have side effects. You are just querying the
state of your object. And since you'd never allow an internally
inconsistent object to be built, there just can't be an exception raised
when you query its state. In other words, if it can potentially raise then
it should be obj.method() instead of object.property.

I think you might have missed my point. I agree properties *shouldn't*
raise exceptions, but they can and do even if unintentionally. I was just
demonstrating the difference between Python 2 and Python 3 wrt how
hasattr() handles exceptions. Python 3 is better (modulo AttributeError) at
letting unintended exceptions bubble up.

A custom __hasattr__ method is a different topic and not related to
exception handling--it's just useful for checking attribute existence
without evaluating the attribute.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to