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
--
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org