ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 20 Mar 2011 21:32:25 -0500How do instantiate a new class properly?https://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?Fri, 18 Mar 2011 21:23:26 -0500https://ask.sagemath.org/question/8013/how-do-instantiate-a-new-class-properly/Comment by Simon for <h2>Background</h2>
<p>I have created a new class <code>FeynmanGraph</code> that is a subclass of <code>Graph</code>. 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.)</p>
<p>The code works. I should mention that I'm editing <code>feynman.sage</code> in a text editor and attaching it to a sage session in a terminal. Once a I create an instance of the class via </p>
<blockquote>
<p><code>G = FeynmanGraph(...)</code></p>
</blockquote>
<p>everything seems to work.</p>
<p>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 <code>feynman</code> and make definitions such as the following?</p>
<blockquote>
<p><code>from feynman import *</code><br/>
<code>def FatY(): # a particular graph</code><br/>
<code>G = FeynmanGraph(...)</code><br/>
<code>...</code><br/>
<code>return G</code></p>
</blockquote>
<p>When I do this, the methods for <code>FeynmanGraph</code> raise all kinds of errors of the type where they don't recognize various functions, such as <code>max</code> or <code>partitions_set</code>.</p>
<h2>Question</h2>
<p>How do I structure the modules and classes so that I can instantiate the class properly?</p>
https://ask.sagemath.org/question/8013/how-do-instantiate-a-new-class-properly/?comment=21963#post-id-21963Hi Sammy, maybe you can post up `feynman.sage` in a gist ( gist.github.com ) or something similar so that people can see what you have tried. In your stackoverflow post ( http://stackoverflow.com/q/5278911/421225 ) 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 ( doxdrum.wordpress.com ) where he steps through his work on a GR module. Also, maybe google/stackoverflow "python super init"Fri, 18 Mar 2011 22:50:32 -0500https://ask.sagemath.org/question/8013/how-do-instantiate-a-new-class-properly/?comment=21963#post-id-21963Answer by Felix Lawrence for <h2>Background</h2>
<p>I have created a new class <code>FeynmanGraph</code> that is a subclass of <code>Graph</code>. 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.)</p>
<p>The code works. I should mention that I'm editing <code>feynman.sage</code> in a text editor and attaching it to a sage session in a terminal. Once a I create an instance of the class via </p>
<blockquote>
<p><code>G = FeynmanGraph(...)</code></p>
</blockquote>
<p>everything seems to work.</p>
<p>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 <code>feynman</code> and make definitions such as the following?</p>
<blockquote>
<p><code>from feynman import *</code><br/>
<code>def FatY(): # a particular graph</code><br/>
<code>G = FeynmanGraph(...)</code><br/>
<code>...</code><br/>
<code>return G</code></p>
</blockquote>
<p>When I do this, the methods for <code>FeynmanGraph</code> raise all kinds of errors of the type where they don't recognize various functions, such as <code>max</code> or <code>partitions_set</code>.</p>
<h2>Question</h2>
<p>How do I structure the modules and classes so that I can instantiate the class properly?</p>
https://ask.sagemath.org/question/8013/how-do-instantiate-a-new-class-properly/?answer=12204#post-id-12204Instead 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.Fri, 18 Mar 2011 22:51:45 -0500https://ask.sagemath.org/question/8013/how-do-instantiate-a-new-class-properly/?answer=12204#post-id-12204Comment by Sammy Black for <p>Instead of "<code>from feynman import *</code>", try "<code>attach feynman.sage</code>" - my guess is that if you use the <code>from ... import</code> syntax then the .sage file is treated like a .py file rather than sage code.</p>
https://ask.sagemath.org/question/8013/how-do-instantiate-a-new-class-properly/?comment=21962#post-id-21962Thanks, Felix. I should have mentioned that I was still having problems even when I renamed it `feynman.py`. I was just trying to mimic the style built into the various Sage (python) modules.Sun, 20 Mar 2011 21:32:25 -0500https://ask.sagemath.org/question/8013/how-do-instantiate-a-new-class-properly/?comment=21962#post-id-21962