Skip to content

inheritance way to implement lazy objects #11

Description

@felixcarmona

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:

from lazy_object_proxy import Proxy

class LazyObject:
    def __new__(cls, *args, **kwargs):
        class_ = type(object.__class__.__name__, (cls,), {})
        class_.__new__ = lambda cls_, *args_, **kwargs_: object.__new__(cls_)
        return Proxy(lambda: class_(*args, **kwargs))

class A(LazyObject):
    def __init__(self, x):
        print("Init")
        self.x = 5

a = A(5)
print('GO')
print(a.x)

result:

GO
Init
5

What do you think about this implementation? do you think I've missed any concern? any way to implement this in C?

Regards

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions