Question on attribute lookup

2 views
Skip to first unread message

Simon King

unread,
Dec 11, 2010, 12:52:24 PM12/11/10
to sage-devel
Hi!

If an attribute of an element is requested, and this attribute is not
directly available, then the attribute lookup in Element.__getattr__
is rather complicated: Using
sage.structure.parent.getattr_from_other_class, it is tested whether
the attribute may be available from the element class of the category
of the element's parent.

There are, however, situations where it is clear to the programmer
that a specific attribute is either defined for one particular
instance, or it is not defined at all. I wonder whether it is possible
to explicitly avoid the complicated attribute lookup in that case,
thereby saving a lot of time.

One example that I found today (though there might be better use
cases):
The default __repr__ method of Sage objects first tests whether the
element has an attribute "__custom_name". It should be clear that, if
the instance itself does not have the attribute, then it shouldn't be
looked up in, for example,
sage.categories.commutative_rings.CommutativeRings.element_class.

If there is no way to circumvent the lookup:
Do you think it is reasonable to change Element.__getattr__ so that it
immediately raises an AttributeError if the requested attribute name
starts with two underscores, but does not end with an underscore?
IIRC, those attributes are supposed to be private (and are subject to
name mangling), so that it may make sense to exclude them from lookup.

A related question:
The __repr__ method of Sage objects does in fact
if hasattr(self,'__custom_name'):
return self.__custom_name

Wouldn't it be slightly faster to do
try:
return self.__custom_name
except AttributeError:
pass

because hasattr(...) does the lookup and catches the attribute error
anyway, so that "return self.__custom_name" merely means to do all the
lookup again?

Best regards,
Simon

Simon King

unread,
Dec 12, 2010, 6:45:27 AM12/12/10
to sage-devel
If someone reads that post:

On 11 Dez., 18:52, Simon King <simon.k...@uni-jena.de> wrote:
> ...
> If there is no way to circumvent the lookup:
> Do you think it is reasonable to change Element.__getattr__ so that it
> immediately raises an AttributeError if the requested attribute name
> starts with two underscores, but does not end with an underscore?
> IIRC, those attributes are supposed to be private (and are subject to
> name mangling), so that it may make sense to exclude them from lookup.

Indeed: If one does what I suggested then all doctests still pass.

> A related question:
> The __repr__ method of Sage objects does in fact
> if hasattr(self,'__custom_name'):
>     return self.__custom_name
>
> Wouldn't it be slightly faster to do
> try:
>     return self.__custom_name
> except AttributeError:
>     pass

It is indeed a lot faster. I changed this and other occurences as
well.

The patch is at ticket #10467, which is ready for review! It provides
two examples (one of them a real computation) where the computation
time drops by almost 1/2 due to the improved attribute lookup.

Cheers,
Simon
Reply all
Reply to author
Forward
0 new messages