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

Commit 4f77364

Browse files
A method to unset category initialisation
1 parent 3b15578 commit 4f77364

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/sage/structure/parent.pyx

+61
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,67 @@ cdef class Parent(category_object.CategoryObject):
503503
print 'hash of {0} changed in Parent._refine_category_ during refinement' \
504504
.format(str(self.__class__))
505505

506+
def _unset_category(self):
507+
"""
508+
Remove the information on ``self``'s category.
509+
510+
NOTE:
511+
512+
This may change ``self``'s class!
513+
514+
EXAMPLES:
515+
516+
Let us create a parent in the category of rings::
517+
518+
sage: class MyParent(Parent):
519+
....: def __init__(self):
520+
....: Parent.__init__(self, category=Rings())
521+
....:
522+
sage: P = MyParent()
523+
sage: P.category()
524+
Category of rings
525+
526+
Of course, its category is initialised::
527+
528+
sage: P._is_category_initialized()
529+
True
530+
531+
We may now refine the category to the category of fields.
532+
Note that this changes the class::
533+
534+
sage: C = type(P)
535+
sage: C == MyParent
536+
False
537+
sage: P._refine_category_(Fields())
538+
sage: P.category()
539+
Category of fields
540+
sage: C == type(P)
541+
False
542+
543+
Now we may have noticed that the category refinement was a
544+
mistake. We do not need to worry, because we can undo category
545+
initialisation totally::
546+
547+
sage: P._unset_category()
548+
sage: P._is_category_initialized()
549+
False
550+
sage: type(P) == MyParent
551+
True
552+
553+
Hence, we can now initialise the parent again in the original
554+
category, i.e., the category of rings. We find that not only
555+
the category, but also theclass of the parent is brought back
556+
to what it was after the original category initialisation::
557+
558+
sage: P._init_category_(Rings())
559+
sage: type(P) == C
560+
True
561+
562+
"""
563+
self._category = None
564+
if not is_extension_type(self.__class__):
565+
while issubclass(self.__class__, Sets_parent_class):
566+
self.__class__ = self.__class__.__base__
506567

507568
# This probably should go into Sets().Parent
508569
@lazy_attribute

0 commit comments

Comments
 (0)