Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can I re-code a function into a method?

I have the following simple function 'add4' stored in a file 'foo.sage':

def add4(x):
    """Adds 4 to x"""
    return x + 4

I can use this in sage by:

sage: attach 'foo.sage'
sage: add4?
Type:       function
Base Class: <type 'function'>
String Form:    <function add4 at 0x4f332a8>
Namespace:  Interactive
File:       Dynamically generated function. No source code available.
Definition: add4(x)
Docstring:
    Adds 4 to x


sage: var('x')
x
sage: add4(x)
x + 4

How can I change my function to a method so that I can call it using the x.add4() syntax? I still want the method to reside in an external file.