Is it possible to proxy a Python str and make join work?

Viewed 802

I'm trying to implement a lazy-evaluated str-like class. What I have now is simething like

class LazyString(object):

    __class__ = str

    def __init__(self, func):
        self._func = func

    def __str__(self):
        return self._func()

which works fine (for my purposes) in most cases, except one: str.join:

' '.join(['this', LazyString(lambda: 'works')])

fails with

TypeError: sequence item 1: expected string, LazyString found

And after some poking around there doesn't seem to be any magic functions available behind this. join seems to be hard-coded inside the core implementation, and only instances of limited built-in type can make it work without actually being a str.

So am I really out of options here, or is there another way that I'm not aware of?

1 Answers
Related