And if you apply the solution in the cython module itself, the methods will only be bound to the class if the module is imported into your session.
For example extending with file myfunctions.pyx : from sage.graphs.graph import Graph as g import types def testme(g): return g g.testme=types.MethodType(testme, None, g) Gives: sage: P=graphs.PetersenGraph() sage: P.testme() Traceback (click to the left of this block for traceback) ... AttributeError: 'Graph' object has no attribute 'testme' sage: import myfunctions as me sage: P.testme() Petersen graph: Graph on 10 vertices sage: me.testme(P) Petersen graph: Graph on 10 vertices On Monday, 18 June 2012 13:17:13 UTC+1, fhivert wrote: > > Ho Nathann, > > On Mon, Jun 18, 2012 at 12:34:15PM +0200, Nathann Cohen wrote: > > Helloooooooooo everybody !!! > > > > Our graph files are getting quite large, and there is in some situations > a > > way to make it shorter : we can define some functions in modules and > import > > them in the Graph class afterwards. For instance in #13073 we do > > > > class Graph: > > ... > > ... > > is_weakly_chordal = sage.graphs.weakly_chordal.is_weakly_chordal > > > > This works well because is_weakly_chordal is defined like that : > > > > def is_weakly_chordal(g): > > > > Hence when G.is_weakly_chordal is called, "self" becomes "g" and > everything > > goes well. > > > > This, however, does not see to be possible with Cython files (if > > graphs/weakly_chordal.py was graphs/weakly_chordal.pyx) and I get the > > following : > > > > TypeError: is_weakly_chordal() takes exactly one argument (0 given) > > > > It looks like in this case the "self" is not given as an argument to the > > function where it expects "g", and so..... it fails :-) > > > > Would anybody know a way around ? > > This look to me like a Cython FAQ: > > > http://wiki.cython.org/FAQ#HowdoIimplementasingleclassmethodinaCythonmodule.3F > > > Here is the solution proposed there: > > import types > import cython_module > > class A(object): > pass > > A.method = types.MethodType(cython_module.optimized_method, None, A) > > Is that what you want ? > > Florent > -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org