Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 4b67d7c

Browse files
committedJun 22, 2014
10668: Explicitly document that Element.__getattr__ can also be useful when refining categories
1 parent fc5b0f1 commit 4b67d7c

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed
 

‎src/sage/rings/polynomial/polynomial_quotient_ring.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,11 @@ class of the category, and store the current class of the quotient
267267
sage: [s for s in dir(Q.category().element_class) if not s.startswith('_')]
268268
['cartesian_product', 'gcd', 'is_idempotent', 'is_one', 'is_unit', 'lcm', 'lift', 'xgcd']
269269
270-
As one can see, the elements are now inheriting additional methods: lcm and gcd. Even though
271-
``Q.an_element()`` belongs to the old and not to the new element class, it still inherits
272-
the new methods from the category of fields::
270+
As one can see, the elements are now inheriting additional
271+
methods: lcm and gcd. Even though ``Q.an_element()`` belongs to
272+
the old and not to the new element class, it still inherits the
273+
new methods from the category of fields, thanks to
274+
:meth:`Element.__getattr__`::
273275
274276
sage: isinstance(Q.an_element(),Q.element_class)
275277
False
@@ -287,7 +289,6 @@ class of the category, and store the current class of the quotient
287289
sage: TestSuite(Q(x)).run()
288290
sage: isinstance(Q(x), Q.element_class)
289291
True
290-
291292
"""
292293
Element = PolynomialQuotientRingElement
293294

‎src/sage/structure/element.pyx

+32-9
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,40 @@ cdef class Element(sage_object.SageObject):
277277

278278
def __getattr__(self, str name):
279279
"""
280-
Let cat be the category of the parent of ``self``. This
281-
method emulates ``self`` being an instance of both ``Element``
282-
and ``cat.element_class``, in that order, for attribute
283-
lookup.
280+
Lookup a method or attribute from the category abstract classes.
284281
285-
NOTE:
282+
Let ``P`` be a parent in a category ``C``. Usually the methods
283+
of ``C.element_class`` are made directly available to elements
284+
of ``P`` via standard class inheritance. This is not the case
285+
any more if the elements of ``P`` are instances of an
286+
extension type. See :class:`Category`. for details.
287+
288+
The purpose of this method is to emulate this inheritance: for
289+
``e`` and element of ``P``, if an attribute or method
290+
``e.foo`` is not found in the super classes of ``e``, it's
291+
looked up manually in ``C.element_class`` and bound to ``e``.
292+
293+
.. NOTES::
294+
295+
- Attributes beginning with two underscores but not ending
296+
with an unnderscore are considered private and are thus
297+
exempted from the lookup in ``cat.element_class``.
286298
287-
Attributes beginning with two underscores but not ending with
288-
an unnderscore are considered private and are thus exempted
289-
from the lookup in ``cat.element_class``.
299+
- The attribute or method is actually looked up in
300+
``P._abstract_element_class``. In most cases this is
301+
just an alias for ``C.element_class``, but some parents,
302+
notably homsets, customizes this to let elements also
303+
inherit from other abstract classes. See
304+
:meth:`Parent._abstract_element_class` and
305+
:meth:`Homset._abstract_element_class` for details.
306+
307+
- This mechanism may also enter into action when the
308+
category of `P` is refined on the fly, leaving
309+
previously constructed elements in an outdated element
310+
class.
311+
312+
See :class:`~sage.rings.polynomial.polynomial_quotient_ring.PolynomialQuotientRing_generic`
313+
for an example.
290314
291315
EXAMPLES:
292316
@@ -330,7 +354,6 @@ cdef class Element(sage_object.SageObject):
330354
Traceback (most recent call last):
331355
...
332356
AttributeError: 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint' object has no attribute '__foo'
333-
334357
"""
335358
if (name.startswith('__') and not name.endswith('_')):
336359
dummy_error_message.cls = type(self)

0 commit comments

Comments
 (0)
This repository has been archived.