Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for the descriptor protocol for lazy_import'ed objects #11224

Closed
nthiery opened this issue Apr 20, 2011 · 5 comments
Closed

Support for the descriptor protocol for lazy_import'ed objects #11224

nthiery opened this issue Apr 20, 2011 · 5 comments

Comments

@nthiery
Copy link
Contributor

nthiery commented Apr 20, 2011

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.

CC: @rishikesha @jasongrout @rlmill

Component: misc

Keywords: sd31

Author: Nicolas M. Thiéry

Reviewer: Mike Hansen

Merged: sage-4.7.2.alpha1

Issue created by migration from https://trac.sagemath.org/ticket/11224

@mwhansen
Copy link
Contributor

comment:1

Attachment: trac_11224-lazy_import-get-nt.patch.gz

Looks good to me.

@mwhansen
Copy link
Contributor

Changed keywords from none to sd31

@mwhansen
Copy link
Contributor

Reviewer: Mike Hansen

@nthiery
Copy link
Contributor Author

nthiery commented Jun 13, 2011

comment:3

Thanks for the review!

@jdemeyer jdemeyer modified the milestones: sage-4.7.1, sage-4.7.2 Jun 21, 2011
@jdemeyer
Copy link
Contributor

Merged: sage-4.7.2.alpha1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants