UserString subclass, 'str' object is not callable
Question: I'm kinda stuck at this code, it's supposed to create a string-like object,
which, upon using, will return internal lambda's value. I use
collections.UserString.

    
    
    from collections import UserString
    class instrx(UserString):
        func = None
        data = ''
        def __init__(self,func):
            self.func = func
        def __getattribute__(self,name):
            print("get",name)
            if name=='data':
                self.data = self.func()
            return UserString.__getattribute__(self,name)
    a = instrx(lambda: 'aa')
    print(a,type(a))
    print(a.lower())
    

Running this code gives me this:

    
    
    Traceback (most recent call last):
      File "C:\path\a.py", line 14, in <module>
        print(a.lower())
      File "C:\Python35\lib\collections\__init__.py", line 1082, in __str__
        def __str__(self): return str(self.data)
      File "C:\path\a.py", line 10, in __getattribute__
        self.data = self.func()
    TypeError: 'str' object is not callable
    

Python version: 3.4, 3.5.


Answer: Okay, I figured this out. It seems that in some functions in UserString (like
`.lower()` in example) `self.__class__(self.data)` construction is used. I did
a little workaround:

    
    
    import time
    from collections import UserString
    class instrx(UserString):
        func = None
        data = ''
        def __init__(self,data,func=None):
            self.data = data
            if func==None:
                self.func = lambda: UserString.__getattribute__(self,'data')
            else:
                self.func = func
        def __getattribute__(self,name):
            if name=='data':
                self.data = str(self.func())
            return UserString.__getattribute__(self,name)
    a = instrx('',func=lambda: time.time())
    print(a,type(a))
    print(a.lower())
    print(a+"test")
    

Works just fine.

