#11999: Renaming lazy attributes
-------------------------+--------------------------------------------------
   Reporter:  SimonKing  |          Owner:  jason   
       Type:  defect     |         Status:  new     
   Priority:  major      |      Milestone:  sage-4.8
  Component:  misc       |       Keywords:          
Work_issues:             |       Upstream:  N/A     
   Reviewer:             |         Author:  Author  
     Merged:             |   Dependencies:          
-------------------------+--------------------------------------------------
 Currently, it is not possible to take an existing lazy attribute and
 assign it to a different name. The reason is that a lazy attribute
 overwrites itself by a usual attribute of its `__name__`. But `__name__`
 is created during initialisation of the lazy attribute. Thus, we have the
 following example:
 {{{
 sage: class Foo(object):
 ....:     @lazy_attribute
 ....:     def _bar(self):
 ....:         return 5
 ....:     bar = _bar
 ....:
 sage: f = Foo()
 sage: f.bar  # it seems to work,...
 5
 sage: f.__dict__['bar'] # ... but the lazy attribute has not become a
 usual attribute
 Traceback (most recent call last):
 ...
 KeyError: 'bar'
 sage: f.__dict__['_bar']
 5
 }}}
 By consequence, `f.bar` is much slower than `f._bar`:
 {{{
 sage: %timeit f.bar
 625 loops, best of 3: 2.78 µs per loop
 sage: %timeit f._bar
 625 loops, best of 3: 185 ns per loop
 }}}

 I suggest to provide lazy attributes with a method `rename()`, returning a
 copy of that lazy attribute under a new name.

 Example, which is a doc test of my patch:
 {{{
 sage: class Foo(object):
 ....:     @lazy_attribute
 ....:     def _bar(self):
 ....:         return 5
 ....:     bar = _bar.rename("bar")
 ....:
 sage: f = Foo()
 sage: f.bar
 5
 sage: f.__dict__['bar']
 5
 sage: %timeit f.bar
 625 loops, best of 3: 184 ns per loop
 sage: %timeit f._bar
 625 loops, best of 3: 184 ns per loop
 }}}

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/11999>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to