How to instantiate in a lazy way?

Slaunger Slaunger at gmail.com
Mon Dec 1 15:01:48 CET 2008


Slaunger wrote:
>
> class PayloadOnDemand(object):
>     """
>     Behaves as a PayloadInstant object, but instantiation is faster
>     as only the position of the payload in the file is stored
>     initially in the object.
>     Only when acessing the initially non-existing data attribute
>     are the data actually read and the attribure created and bound to
>     the instance.
>     This will actually be a little slower than in PayloadInstant as
>     the correct file position
>     has to be seeked out first.
>     On later calls the object has as efficient attribute access as
>     PayloadInstant
>     """
>
>     @classmethod
>     def read_from_file(cls, f, size):
>         pos = f.tell()
>         f.seek(pos + size) #Skip to end of payload
>         return cls(pos)

Extend with ref to file instead:
          return cls(f, pos)
>
>     # I probably need some __getattr__ or __getattribute__ magic
>     # there...??

To answer my own rethorical question I guess I should do something
like this

    def __getattr__(self, attr_name):
        """
        Only called if attr_name is not in the __dict__ for the
instance
        """
        if attr_name == 'data':
            self.__dict__[attr_name] = read_data(self.f,
self.file_position)

>
>     def __init__(self, a_file_position):
>         self.file_position = a_file_position
>
and then I need to also store a reference to the file in the
constructor...

    def __init__(self, a_file, a_file_position):
        self.f = a_file
        self.file_position = a_file_position

Have I understood correctly how to to it the on demand way?

-- Slaunger



More information about the Python-list mailing list