ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Nov 2011 11:57:28 -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.
rtrwalkerWed, 16 Nov 2011 11:57:28 -0600http://ask.sagemath.org/question/8482/How do instantiate a new class properly?http://ask.sagemath.org/question/8013/how-do-instantiate-a-new-class-properly/## Background ##
I have created a new class `FeynmanGraph` that is a subclass of `Graph`. It has some extra methods, and the module in which it lives also has some extra functions. The most important method for my purposes searches for possible colorings of the vertices that satisfy certain combinatorial criteria that have to do with spanning trees. (The details are not important here.)
The code works. I should mention that I'm editing `feynman.sage` in a text editor and attaching it to a sage session in a terminal. Once a I create an instance of the class via
> `G = FeynmanGraph(...)`
everything seems to work.
Now, I created a module in which I would like to define specific graphs, with specific labeling of the vertices and edges, as well as plot positioning information. Shouldn't I be able to import the module `feynman` and make definitions such as the following?
> `from feynman import *`<br>
> `def FatY(): # a particular graph`<br>
> `G = FeynmanGraph(...)`<br>
> `...`<br>
> `return G`
When I do this, the methods for `FeynmanGraph` raise all kinds of errors of the type where they don't recognize various functions, such as `max` or `partitions_set`.
## Question ##
How do I structure the modules and classes so that I can instantiate the class properly?Sammy BlackFri, 18 Mar 2011 21:23:26 -0500http://ask.sagemath.org/question/8013/