Opened 9 years ago

Closed 9 years ago

#11224 closed enhancement (fixed)

Support for the descriptor protocol for lazy_import'ed objects

Reported by: nthiery Owned by: jason
Priority: major Milestone: sage-4.7.2
Component: misc Keywords: sd31
Cc: rishi, jason, rlm Merged in: sage-4.7.2.alpha1
Authors: Nicolas M. Thiéry Reviewers: Mike Hansen
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

This patch enable to lazy import objects using the descriptor protocol (readonly at this point: just the get method).

A sample application taken from the doc::

Here we show how to take a function in a module, and lazy
import it as a method of a class. For the sake of this
example, we add manually a function in sage.all::

    sage: def my_method(self): return self
    sage: sage.all.my_method = my_method

Now we lazy import it as a method of a new class ``Foo``::

    sage: from sage.misc.lazy_import import LazyImport
    sage: class Foo:
    ...       my_method = LazyImport('sage.all', 'my_method')

Now we can use it as a usual method::

    sage: Foo().my_method()
    <__main__.Foo instance at ...>
    sage: Foo.my_method
    <unbound method Foo.my_method>
    sage: Foo().my_method
    <bound method Foo.my_method of <__main__.Foo instance at ...>>

Currently, ``my_method`` remains a lazy imported object in the
class dictionary, even when it has already been used, which is
not as efficient as it could be (but see #11003)::

    sage: type(Foo.__dict__["my_method"])
    <type 'sage.misc.lazy_import.LazyImport'>

In practice, I needed this feature for further work on categories and functorial constructions, around #11111 and #10963.

Attachments (1)

trac_11224-lazy_import-get-nt.patch (1.9 KB) - added by nthiery 9 years ago.

Download all attachments as: .zip

Change History (6)

Changed 9 years ago by nthiery

comment:1 Changed 9 years ago by mhansen

  • Keywords sd31 added
  • Reviewers set to Mike Hansen
  • Status changed from new to needs_review

Looks good to me.

comment:2 Changed 9 years ago by mhansen

  • Status changed from needs_review to positive_review

comment:3 Changed 9 years ago by nthiery

Thanks for the review!

comment:4 Changed 9 years ago by jdemeyer

  • Milestone changed from sage-4.7.1 to sage-4.7.2

comment:5 Changed 9 years ago by jdemeyer

  • Merged in set to sage-4.7.2.alpha1
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.