Python:“str"对象不可调用 [英] Python: 'str' object is not callable

查看:102
本文介绍了Python:“str"对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

f = open('BigTestFile','w');
str = '0123456789'
for i in range(100000000):
    if i % 1000000 == 0:
        print(str(i / 1000000) + ' % done')
    f.write(str)
f.close()

当我运行它时,我得到这个TypeError:

When I run it, I get this TypeError:

Traceback (most recent call last):
  File "gen_big_file.py", line 8, in <module>
        print(str(i / 1000000) + ' % done')
TypeError: 'str' object is not callable

这是为什么?如何修复?

Why is that? How to fix?

推荐答案

将变量称为 str 以外的其他内容.它掩盖了 str 内置函数.

Call the variable something other than str. It is shadowing the str built in function.

这篇关于Python:“str"对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆