str is not callable error in python

101 阅读2分钟

当运行一个包含 key.update(str(ak)) 代码的 Python 程序时,出现 str is not callable 错误。这通常是由于将内置函数 str() 名称重新分配给一个变量,导致当尝试再次使用 str() 函数时,出现 str 不是可调用对象。

  1. 解决方案 为了解决这个问题,需要避免将 str 用作变量名,因为它在 Python 中是内置函数的名称。为了清楚地使用内置函数 str() 来将对象转换为字符串,需要使用不同的变量名,例如 stringtext

以下是修改后的代码,其中 str 变量名被 string 代替:

for line in lines:
  string = line.strip()
  if (string != ""):
    for i in range(0, 4):
      t.getNode(i).addNoiseTraceReading(val)

这样修改后,就不会再出现 str is not callable 错误,因为 string 不再与内置函数 str() 冲突。

代码示例

以下是完整的修改后代码:

import sys
import md5
from TOSSIM import *
from RadioCountMsg import *

t = Tossim([])  # The Tossim object is defined here

m = t.mac()  # The mac layer is defined here, in which the communication takes place
r = t.radio()  # The radio communication link object is defined here, as the communication needs Rf frequency to transfer

t.addChannel("RadioCountToLedsC", sys.stdout)  # The various channels through which communication will take place
t.addChannel("LedsC", sys.stdout)

# The no of nodes that would be required in the simulation has to be entered here
print("enter the no of nodes you want ")
n = input()

for i in range(0, n):
  m = t.getNode(i)
  m.bootAtTime((31 + t.ticksPerSecond() / 10) * i + 1)
  # The booting time is defined so that the time at which the node would be booted is given

f = open("topo.txt", "r")  # The topography is defined in topo.txt so that the RF frequencies of the transmission between nodes are are set
lines = f.readlines()
for line in lines:
  s = line.split()
  if (len(s) > 0):
    if (s[0] == "gain"):
      r.add(int(s[1]), int(s[2]), float(s[3]))  # The topogrography is added to the radio object

noise = open("meyer-heavy.txt", "r")  # The noise model is defined for the nodes
lines = noise.readlines()
for line in lines:
  string = line.strip()  # Use a different variable name here
  if (string != ""):
    val = int(string)
    for i in range(0, 4):
      t.getNode(i).addNoiseTraceReading(val)

for i in range(0, n):
  t.getNode(i).createNoiseModel()  # The noise model is created for each node

for i in range(0, n):
  t.runNextEvent()
fk = open("key.txt", "w")
for i in range(0, n):
  if i == 0:
    key = raw_input()
    fk.write(key)
    ak = key
  key = md5.new()
  key.update(ak)
  ak = key.digest()
  fk.write(ak)
fk.close()
fk = open("key.txt", "w")
plaint = open("pt.txt")
for i in range(0, n):
  msg = RadioCountMsg()
  msg.set_counter(7)
  pkt = t.newPacket()  # A packet is defined according to a certain format
  print("enter message to be transported")
  ms = raw_input()  # The message to be transported is taken as input
  # The RC5 encryption has to be done here
  plaint.write(ms)
  pkt.setData(msg.data)
  pkt.setType(msg.get_amType())
  pkt.setDestination(i + 1)  # The destination to which the packet will be sent is set

  print("Delivering " + " to", i + 1)
  pkt.deliver(i + 1, t.time() + 3)

fk.close()

print("the key to be displayed")
ki = raw_input()
fk = open("key.txt")

for i in range(0, n):
  if i == ki:
    ms = fk.readline()

for i in range(0, n):
  msg = RadioCountMsg()
  msg.set_counter(7)
  pkt = t.newPacket()
  msg.data = ms
  pkt.setData(msg.data)
  pkt.setType(msg.get_amType())
  pkt.setDestination(i + 1)
  pkt.deliver(i + 1, t.time() + 3)

# The key has to be broadcasted here so that the decryption can take place

for i in range(0, n):
  t.runNextEvent();

在运行此修改后的代码时,不再会出现 str is not callable 错误。