> Johan's suggestion is very intriguing but, unless I am completely mistaken, 
> it does not work in python. If I understand correctly if O is an instance of 
> Foo and O does not have the attribute x then python looks for Foo().x

You're right, I seem to have confused myself with deleting attributes of
subclasses.

Another possibility is to create micro-classes for each method which
only certain matrices have, and then dynamically monkey-patch each
constructed matrix to inherit from the micro-classes it should.
Unfortunately this requires assigning to o.__class__, which
doesn't seem possible when o is an instance of a Cython class.

Otherwise, the code below could work, I believe. The `extend_instance` is taken 
from
http://stackoverflow.com/questions/8544983/dynamically-mixin-a-base-class-to-an-instance-in-python.

    def extend_instance(obj, cls):
        """Apply mixins to a class instance after creation"""
        base_cls = obj.__class__
        base_cls_name = obj.__class__.__name__
        obj.__class__ = type(base_cls_name, (base_cls, cls),{})

    class IntegerMatrixTwoColumn(SageObject):
        def magic_method(self):
            return 42

    def MyMatrix(*args):
        M = matrix(*args)
        if M.ncols() == 2 and M.base_ring() == ZZ:
            extend_instance(M, IntegerMatrixTwoColumn)


    sage: M = MyMatrix(ZZ, 2, 2, [1,2,3,4])
    sage: M.magic_method()
    42

Best,
Johan

-- 
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