Ask Your Question

How do instantiate a new class properly?

asked 2011-03-18 21:23:26 -0500

Sammy Black gravatar image


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 *
def FatY(): # a particular graph
    G = FeynmanGraph(...)
    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.


How do I structure the modules and classes so that I can instantiate the class properly?

edit retag flag offensive close merge delete


Hi Sammy, maybe you can post up `feynman.sage` in a gist ( ) or something similar so that people can see what you have tried. In your stackoverflow post ( ) you were having trouble with your `__init__`, since you weren't calling the underlying `Graph.__init__`. Maybe you should have a look at some of doxdrum's posts ( ) where he steps through his work on a GR module. Also, maybe google/stackoverflow "python super init"

Simon gravatar imageSimon ( 2011-03-18 22:50:32 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2011-03-18 22:51:45 -0500

Felix Lawrence gravatar image

Instead of "from feynman import *", try "attach feynman.sage" - my guess is that if you use the from ... import syntax then the .sage file is treated like a .py file rather than sage code.

edit flag offensive delete link more


Thanks, Felix. I should have mentioned that I was still having problems even when I renamed it ``. I was just trying to mimic the style built into the various Sage (python) modules.

Sammy Black gravatar imageSammy Black ( 2011-03-20 21:32:25 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-03-18 21:23:26 -0500

Seen: 275 times

Last updated: Mar 18 '11