Inherit Method but keep Documentation?

71 views
Skip to first unread message

Michael Jung

unread,
Mar 18, 2020, 5:04:44 PM3/18/20
to sage-devel
Dear developers,
to reduce redundancies in the SageManifolds code, we plan to inherit most methods and classes from a (mathematically) more general setup.
Still, the current documentation is mandatory. Is it possible to establish new documentations for inherited methods?

An example:

class Mother(SageObject):
   
def my_method(self):
        r
"""
        Some Documentation.
        """"
        return 'Bam!'

class Daughter(Mother):
    pass

For the class "Daughter", the method "my_method" shall have a separate self-containing documentation.

Best regards
Michael

Michael Orlitzky

unread,
Mar 18, 2020, 7:41:38 PM3/18/20
to sage-...@googlegroups.com
On 3/18/20 5:04 PM, Michael Jung wrote:
> Dear developers,
> to reduce redundancies in the SageManifolds code, we plan to inherit
> most methods and classes from a (mathematically) more general setup.
> Still, the current documentation is mandatory. Is it possible to
> establish new documentations for inherited methods?

I tried, and I don't think so.

If you don't override the method, then the subclass method will
literally point to the superclass method, implying that they both have
to have the same __doc__.

Michael Jung

unread,
Mar 18, 2020, 10:58:11 PM3/18/20
to sage-...@googlegroups.com

Damn it. Then I another question: Would it cause a slow-down if I overwrite the method with something like

def my_method(self):
    r"""
    New Documentation
    """
    Mother.my_method(self)

Best,
Michael


Virenfrei. www.avast.com

David Roe

unread,
Mar 19, 2020, 12:22:31 AM3/19/20
to sage-devel
On Wed, Mar 18, 2020 at 10:58 PM Michael Jung <mic...@uni-potsdam.de> wrote:

Damn it. Then I another question: Would it cause a slow-down if I overwrite the method with something like

def my_method(self):
    r"""
    New Documentation
    """
    Mother.my_method(self)


This will take small amount of time (on the order of 150ns), but that time will probably be dwarfed by whatever Mother.my_method is doing.  I don't see an alternative if you want to have different documentation.
David

Best,
Michael

Am 19.03.2020 um 00:41 schrieb Michael Orlitzky:
On 3/18/20 5:04 PM, Michael Jung wrote:
Dear developers,
to reduce redundancies in the SageManifolds code, we plan to inherit
most methods and classes from a (mathematically) more general setup.
Still, the current documentation is mandatory. Is it possible to
establish new documentations for inherited methods?
I tried, and I don't think so.

If you don't override the method, then the subclass method will
literally point to the superclass method, implying that they both have
to have the same __doc__.


Virenfrei. www.avast.com

--
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+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/39892653-0f84-5c96-a12f-ac378f8e535b%40uni-potsdam.de.

John H Palmieri

unread,
Mar 19, 2020, 1:13:56 AM3/19/20
to sage-devel
What about

my_method = Mother.my_method
my_method
.__doc__ = "new docstring"


Does that do what you want?

To unsubscribe from this group and stop receiving emails from it, send an email to sage-...@googlegroups.com.

Simon King

unread,
Mar 19, 2020, 3:47:40 AM3/19/20
to sage-...@googlegroups.com
Hi!

On 2020-03-19, John H Palmieri <jhpalm...@gmail.com> wrote:
> What about
>
> my_method = Mother.my_method
> my_method.__doc__ = "new docstring"
>
>
> Does that do what you want?

Probably not. Wouldn't that be the same as
Mother.my_method.__doc__ = "new docstring"
?

Best regards,
Simon

Michael Orlitzky

unread,
Mar 19, 2020, 9:23:55 AM3/19/20
to sage-...@googlegroups.com
On 3/19/20 3:47 AM, Simon King wrote:
>
> Probably not. Wouldn't that be the same as
> Mother.my_method.__doc__ = "new docstring"
> ?
>

Yeah, that's what I tried, and it overwrites the parent method's docstring.

Simon King

unread,
Mar 19, 2020, 9:33:46 AM3/19/20
to sage-...@googlegroups.com
Hi Michael,

perhaps the code in src/sage/docs/instancedoc.pyx is helpful for you?

I see several problems:
1. It is possible to override the __doc__ of an anbound method, but this
would override the __doc__ also in the parent class:

sage: class A:
....: def my_method(self):
....: "Some doc"
....: pass
....:
sage: class B(A): pass
sage: B.my_method.__doc__ = "Some other doc"
sage: A.my_method.__doc__
'Some other doc'

2. It is impossible (without Cython, at least) to override the __doc__
of a bound method:

sage: a = A()
sage: a.my_method.__doc__ = "Some more doc"
...
AttributeError: attribute '__doc__' of 'method' objects is not writable

3. I thought a potential solution would be to create a subclass
FlexibleDocMethodType of types.MethodType that uses the instancedoc
decorator, say by accessing some attribute of the_method.__self__ that
holds the new doc. "Usual" methods would then (via some decorator) be
replaced by instances of FlexibleDocMethodType. But unfortunately it
seems that types.MethodTypes cannot be subclassed:

sage: from types import MethodType
sage: class FlexibleDocMethodType(MethodType):
....: pass
....:
...
TypeError: type 'method' is not an acceptable base type

Best regards,
Simon

Nils Bruin

unread,
Mar 19, 2020, 6:07:26 PM3/19/20
to sage-devel
There is a way of "copying" a function object, so that you can change the documentation on it.
You should just not do it because it's horribly hacky.

def f(a):
   
"doc of f"
   
return 0

import types
g
=types.FunctionType(f.__code__,f.__globals__)
g
.__doc__="doc of g"

g
.__doc__
f
.__doc__

I think the original doc is actually stored on the __code__ object, so you could also copy that and change the docstring there, but the override on functions seems to work OK. types.CodeType could be employed for that probably.
Reply all
Reply to author
Forward
0 new messages