AttributeError:“str”对象没有属性“\u模块”

2024-05-29 07:36:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在测试这个缓存方法/代码: http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/?c=15348

在某些情况下,我得到这个(或类似的)错误: “AttributeError:'str'对象没有属性'模块'”

下面是一些代码示例,它们工作正常:

if __name__ == '__main__':
@lru_cacheItem(maxsize=20)
def f(x, y):
    return 3*x+y

domain = range(5)
from random import choice
for i in range(1000):
    r = f(choice(domain), choice(domain))

print('Hits:{0}'.format(f.hits), 'Misses:{0}'.format(f.misses))

@lfu_cacheItem(maxsize=20)
def f(x, y):
    return 3*x+y

domain = range(5)
from random import choice
for i in range(1000):
    r = f(choice(domain), choice(domain))

print('Hits:{0}'.format(f.hits), 'Misses:{0}'.format(f.misses))  


@lru_cacheItem(maxsize=20)
def myString(a, b):
    return '{0} and {1}'.format(a, b)

a = 'crap'
b = 'shit'
for i in range(1000):
    r = myString(a, b)

print('Hits:{0}'.format(myString.hits), 'Misses:{0}'.format(myString.misses)) 

但这并不意味着:

if __name__ == '__main__':    
class P4client(object):
    def __init__(self):
        pass

    def checkFileStats(self, filePath):
        results = 'The filepath: {0}'.format(filePath)
        print results
        return results

p4client = P4client()

filePath = (r"C:\depot\tester.tga")

@lfu_cacheItem            
def p4checkFileStats(filePath):
    '''Will cache the fstats return'''
    p4checkResults = p4client.checkFileStats(filePath)
    return p4checkResults    

p4checkFileStats(filePath)

我不知道怎么解决这个。。。这似乎是functools中的一个问题,我假设我正在包装的函数中调用一个类/方法会对这个事实造成什么影响?


Tags: informatforreturndomaindefrangeprint

热门问题