ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Nov 2011 13:01:30 -0600How can I re-code a function into a method?http://ask.sagemath.org/question/8482/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.
Wed, 16 Nov 2011 11:57:28 -0600http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/Comment by DSM for <p>I have the following simple function 'add4' stored in a file 'foo.sage':</p>
<pre><code>def add4(x):
"""Adds 4 to x"""
return x + 4
</code></pre>
<p>I can use this in sage by:</p>
<pre><code>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
</code></pre>
<p>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. </p>
http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20883#post-id-20883Sure. FYI, though, if you *do* have a particular new object you want, then turning a function into a method is trivial (you simply put the function in the class and add a new argument traditionally called "self".) So if you hit any problems along the way, just describe what your goal is and one of the regulars will be happy to help figure out a way to get there. (It could be that a class/method solution is the way to go, but that would require more details.)Wed, 16 Nov 2011 13:01:30 -0600http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20883#post-id-20883Comment by rtrwalker for <p>I have the following simple function 'add4' stored in a file 'foo.sage':</p>
<pre><code>def add4(x):
"""Adds 4 to x"""
return x + 4
</code></pre>
<p>I can use this in sage by:</p>
<pre><code>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
</code></pre>
<p>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. </p>
http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20884#post-id-20884Thanks for the link. The material is beyond my current ability so I think I'll just stick to functions.Wed, 16 Nov 2011 12:49:44 -0600http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20884#post-id-20884Comment by DSM for <p>I have the following simple function 'add4' stored in a file 'foo.sage':</p>
<pre><code>def add4(x):
"""Adds 4 to x"""
return x + 4
</code></pre>
<p>I can use this in sage by:</p>
<pre><code>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
</code></pre>
<p>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. </p>
http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20887#post-id-20887Methods are "subfunctions" of objects, though. What object in particular do you want to attach your function to?Wed, 16 Nov 2011 12:02:08 -0600http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20887#post-id-20887Comment by DSM for <p>I have the following simple function 'add4' stored in a file 'foo.sage':</p>
<pre><code>def add4(x):
"""Adds 4 to x"""
return x + 4
</code></pre>
<p>I can use this in sage by:</p>
<pre><code>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
</code></pre>
<p>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. </p>
http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20885#post-id-20885No, I don't think you can do that: SageObject isn't that kind of parent. Frankly, without patching Sage, it's hard enough even to get subclassing Expression to behave nicely. (See, e.g., http://groups.google.com/group/sage-support/browse_thread/thread/f749ba3cd079c6f4?pli=1).Wed, 16 Nov 2011 12:35:45 -0600http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20885#post-id-20885Comment by rtrwalker for <p>I have the following simple function 'add4' stored in a file 'foo.sage':</p>
<pre><code>def add4(x):
"""Adds 4 to x"""
return x + 4
</code></pre>
<p>I can use this in sage by:</p>
<pre><code>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
</code></pre>
<p>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. </p>
http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20886#post-id-20886Is it possible to 'attach' it to SageObject or do I have to be more specific? My example is simple but later i would want to do more complicated actions.Wed, 16 Nov 2011 12:22:29 -0600http://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/?comment=20886#post-id-20886