#12978: conditionally_defined decorator for methods and other attributes
-------------------------------------------------+-------------------------
       Reporter:  nthiery                        |        Owner:  jason
           Type:  enhancement                    |       Status:  new
       Priority:  major                          |    Milestone:  sage-
      Component:  misc                           |  wishlist
       Keywords:  categories,                    |   Resolution:
  conditionally_defined                          |    Merged in:
        Authors:                                 |    Reviewers:
Report Upstream:  N/A                            |  Work issues:
         Branch:                                 |       Commit:
   Dependencies:  #15056                         |     Stopgaps:
-------------------------------------------------+-------------------------

Comment (by SimonKing):

 With the latest version of #15056 and the patch that I have just attached,
 one can do (that's currently the only doctest):
 {{{
     sage: from sage.misc.conditionally_defined import
 conditionally_defined
     sage: class A(object):
     ....:     @cached_method
     ....:     def method1(self, x):
     ....:         print "calling cached method1"
     ....:         return -x
     ....:     def method2(self, x):
     ....:         print "calling method2"
     ....:         return 2*x
     sage: class B(A):
     ....:     @conditionally_defined(lambda x: x.con>0)
     ....:     def method1(self, x):
     ....:         print "calling uncached method1"
     ....:         return x^2
     ....:     @cached_method
     ....:     @conditionally_defined(lambda x: x.con<0)
     ....:     def method2(self, x):
     ....:         print "calling alternative method2"
     ....:         return 2+x
     sage: x = B()
     sage: y = B()
     sage: x.con = 1
     sage: y.con = -1
 }}}

 The method `method1` of the two instances `x` or `y` is either a usual
 method,
 or a cached method, depending on the value of the attribute `.con`:
 {{{
     sage: x.method1
     <bound method B.method1 of <__main__.B object at ...>>
     sage: y.method1
     Cached version of <bound method B.method1 of <__main__.B object at
 ...>>
     sage: x.method1(3)
     calling uncached method1
     9
     sage: x.method1(3)
     calling uncached method1
     9
     sage: y.method1(3)
     calling cached method1
     -3
     sage: y.method1(3) is y.method1(3)
     True
 }}}
 The method `method2` is cached, but the underlying function of the cached
 method is conditionally defined::
 {{{
     sage: x.method2
     Cached version of <bound method B.method2 of <__main__.B object at
 ...>>
     sage: y.method2
     Cached version of <bound method B.method2 of <__main__.B object at
 ...>>
     sage: x.method2(3)
     calling method2
     6
     sage: x.method2(3) is x.method2(3)
     True
     sage: y.method2(3)
     calling alternative method2
     5
     sage: y.method2(3) is y.method2(3)
     True
 }}}

 Do you agree that this seems useful?

--
Ticket URL: <http://trac.sagemath.org/ticket/12978#comment:17>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" 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-trac.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to