-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
inheritance way to implement lazy objects #11
Comments
Is your intention roughly "delay object initialization" or "delay object creation"? It's not clear what's wrong with the regular way ( |
Doing something special in constructor ( Otherwise just use a class decorator: def lazy(klass):
def lazy_class(*args, **kwargs):
return Proxy(lambda: klass(*args, **kwargs))
lazy_class.__name__ = "lazy_" + klass.__name__
return lazy_class
@lazy
class A(object):
.... |
Now that I look again, your example seems fine (I didn't test it so it's a loose measure of "fine" 😁). You should replace |
thank you for your quick answer, which way is better? (I implemented my own because I didn't find your alternative in the doc or in tests, is there any reason to not include it?) |
I'm doing some tests: from lazy_object_proxy import Proxy
class LazyObject:
def __new__(cls, *args, **kwargs):
class_ = type(object.__name__, (cls,), {})
class_.__new__ = lambda cls_, *args_, **kwargs_: object.__new__(cls_)
return Proxy(lambda: class_(*args, **kwargs))
class B(LazyObject):
def __init__(self, x):
print("Init")
self.x = x
b = B(5)
print('GO')
print(b.x)
print(isinstance(b, B)) returns:
and from lazy_object_proxy import Proxy
def lazy(klass):
def lazy_class(*args, **kwargs):
return Proxy(lambda: klass(*args, **kwargs))
lazy_class.__name__ = "lazy_" + klass.__name__
return lazy_class
@lazy
class A:
def __init__(self, x):
print("Init")
self.x = x
a = A(5)
print('GO')
print(a.x)
print(isinstance(a, A)) returns:
|
You're right. I suppose we could have a metaclass that roughly does what you do in your example in a |
Sounds good, thanks for your time, it's great to have contributors like you. edit: btw: from lazy_object_proxy import Proxy
def lazy(cls):
class Lazy:
def __new__(cls, *args, **kwargs):
original_class = type(object.__name__, (cls,), {})
original_class.__new__ = lambda cls_, *args_, **kwargs_: object.__new__(cls_)
return Proxy(lambda: original_class(*args, **kwargs))
lazy_class = type(cls.__name__, (cls, Lazy), {})
return lazy_class
@lazy
class B:
def __init__(self, x):
print("Init")
self.x = x
b = B(5)
print('GO')
print(b.x)
print(isinstance(b, B)) output:
|
Fixes #22 In order to enable environment variables to be mocked using either monkeypatch or mock.patch we enable lazy initialization of the AuthController class. - Adds a lazy decorator and applies it to the AuthController following ionelmc/python-lazy-object-proxy#11 - Favour yield over request.add_finalizer for clean up. - Adds tests
Fixes #22 In order to enable environment variables to be mocked using either monkeypatch or mock.patch we enable lazy initialization of the AuthController class. - Adds a lazy decorator and applies it to the AuthController following ionelmc/python-lazy-object-proxy#11 - Favour yield over request.add_finalizer for clean up. - Adds tests
I want to implement a new more unattended way to proxy objects, inheriting a LazyObject class instead of doing a = Proxy(lambda: A(5))
I'm doing this:
result:
What do you think about this implementation? do you think I've missed any concern? any way to implement this in C?
Regards
The text was updated successfully, but these errors were encountered: