On Mon, Mar 17, 2014 at 06:36:47PM -0400, Mark Shimozono wrote:
> I have been needing to define lots of morphisms using pure functions.
> 
> Here's a simple example: try to give every free module an identity morphism
> in a way that passes pickling.
> 
> {{{
> 
> class ModulesWithBasis(Category_over_base_ring):
> ...
>     class ParentMethods:
> ...
>         def _identity_map(self):
>             def the_id(x):
>                 return x
>             import __main__; __main__.the_id = the_id
>             return SetMorphism(Hom(self,self,self.category()),the_id)
> 
> }}}
> 
> This isn't going to work if the method is called multiple times
> because the global reference will get overwritten.

Oh, sorry for the confusion. If the function is already defined in
some Python module (but not within a function another), it's right
away picklable, so there is nothing to do.  The example above would
become:

{{{
def id(x): return x
class ...

         def _identity_map(self):
             return SetMorphism(Hom(self,self,self.category()),id)
}}}

This __main__ trick is only for when you define python
functions "interactively" (e.g. in doctests), and you want to fake
them being picklable for TestSuite's pickling tests.

Cheers,
                                Nicolas
--
Nicolas M. ThiĆ©ry "Isil" <[email protected]>
http://Nicolas.Thiery.name/

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to