Skip to content

Dynamic attributes for symbolic expressions #9556

@simon-king-jena

Description

@simon-king-jena

Let e be a symbolic expression. It may happen that e.operator() has a certain callable attribute, say, foo, that is not a method of Function. In this situation, one would like to use e.foo(), which is supposed to return e.operator().foo(*e.operands()) - apparently this is useful for working with hypergeometric functions (#2516).

Example

       sage: from sage.symbolic.function import BuiltinFunction
        sage: class TFunc(BuiltinFunction):
        ....:     def __init__(self):
        ....:         BuiltinFunction.__init__(self, 'tfunc', nargs=1)
        ....:
        ....:     class EvaluationMethods:
        ....:         def argp1(self, x):
        ....:             '''
        ....:             Some documentation about a bogus function.
        ....:             '''
        ....:             return x+1
        ....:
        ....:         @property
        ....:         def foo(self):
        ....:             return 5
        ....:
        sage: tfunc = TFunc()
        sage: e = tfunc(x); e
        tfunc(x)
        sage: type(e)
        <class '__main__.Expression_with_dynamic_methods'>
        sage: e.argp1()
        x + 1
        sage: e.foo
        5
        sage: x.argp1()
        Traceback (most recent call last):
        ...
        AttributeError: 'sage.symbolic.expression.Expression' object has no
        attribute 'argp1'
        sage: t = (e + 1).op[0]; t
        tfunc(x)
        sage: t
        tfunc(x)
        sage: type(t)
        <class '__main__.Expression_with_dynamic_methods'>
        sage: t.argp1()
        x + 1
        sage: import sagenb.misc.support as s
        sage: s.completions('t.argp', globals(), system='python')
        ['t.argp1']
        sage: t.argp1.__doc__.strip()
        'Some documentation about a bogus function.'

Apply: attachment: trac_9556-dynamic_class_everywhere.patch

CC: @burcin @kcrisman @vbraun @eviatarbach

Component: symbolics

Keywords: symbolic expression dynamic attribute sd48

Author: Burcin Erocal, Simon King, Mike Hansen

Reviewer: Volker Braun

Merged: sage-5.11.beta3

Issue created by migration from https://trac.sagemath.org/ticket/9556

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions