简体   繁体   English

python:TypeError:'str'对象不可调用

[英]python: TypeError: 'str' object is not callable

i'm trying to load a binary file with pickle that has a record in a list, like so 我试图用泡菜加载二进制文件,该泡菜在列表中有一条记录,像这样

import pickle
class player_energy_usage():
def __init__(self):
    self.weapons = 25
    elf.shields = 25
    self.life_support = 25
    self.engines = 25

def p_eu_init():
    global p_energy   
    p_energy = []
    player_ship_energy = player_energy_usage()
    p_energy.append(player_ship_energy)
    pickle.dump(p_energy,open('p_energy.dat','wb'))

p_eu_init()
pickle.load('rb'('p_energy.dat'))
print('Weapons are using {0}% of energy'.format(p_energy[0].weapons))
print('Shields are using {0}% of energy'.format(p_energy[0].shields))
print('Life Support is using {0}% of energy'.format(p_energy[0].life_support))
print('Engines is using {0}% of energy'.format(p_energy[0].engines))

However i get a type error, 但是我遇到类型错误,

Traceback (most recent call last):
File "E:/Python texted based game/Tests/file loading test.py", line 18, in <module>
pickle.load('rb'('p_energy.dat'))
TypeError: 'str' object is not callable

thanks for the help. 谢谢您的帮助。

That is not the correct syntax. 那不是正确的语法。 It should be instead: 应该改为:

p_energy = pickle.load(open('p_energy.dat', 'rb'))

What you're actually doing is: 您实际上正在做的是:

'rb'('p_energy.dat') is trying to call the str object 'rb' with an argument of 'p_energy.dat' , which is why you get the error 'str' object is not callable . 'rb'('p_energy.dat')试图使用参数'p_energy.dat'调用str对象'rb' 'p_energy.dat' ,这就是为什么您收到错误消息'str' object is not callable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM