【问题标题】:TypeError: 'tuple' object is not callable when trying to call methodTypeError:尝试调用方法时“元组”对象不可调用
【发布时间】:2015-03-11 16:50:33
【问题描述】:

这是我到目前为止所做的:

# -*- coding: cp1252 -*-
import time

class Item():
    def __init__(self, name, description, base_value):
        self.name = name
        self.description = description
        self.ingredients = ingredients
        self.base_value = value
    def __str__(self):
        return format(self.name, self.description, self.ingredients, self.base_value)


class Metal(Item):
    def __init__(self, name, description, ingredients, base_value):
        self.smelt_time = smelt_time
        self.smelted = smelted
    def __str__(self):
        return format(self.name, self.description, self.ingredients, self.base_value, self.smelt_time, self.smelted)

class Bronze_Ingot(Metal):
    def __init__(self):
        self.name = "Bronze Ingot",
        self.description = "A refined ingot of bronze."
        #self.ingredients = Tin_Ore(1)+Copper_Ore(1) <--- I will get these lines working later...
        self.base_value = 33
        self.smelt_time = 15
        self.smelted = ()            

class Fuel(Item):
    def __init__(self, name, description, ingredients, base_value):
        self.fuel_time = fuel_time
    def __str__(self):
        return format(self.name, self.description, self.ingredients, self.base_value, self.fuel_time)

class Cloth(Fuel):
    def __init__(self):
        self.name = "Cloth",
        self.description = "A piece of cotton cloth."
        #self.ingredients = 2 Cotton <--- I will get these lines working later... 
        self.base_value = 2
        self.fuel_time = 5

但是我在使用这个功能时遇到了很大的麻烦......

    def smelted(Fuel, Metal):
        if (Fuel.fuel_time - Metal.smelt_time) > 0:
            time.sleep(1)
            print "Smelting", Metal.name, "..."
            time.sleep(Metal.smelt_time)
            print "Time to forge!"

问题或多或少是让它发挥作用。我和我的朋友已经尝试了 我们 在运行此功能时可以想到的所有方法,但无济于事。这是我们最近的尝试:

from Smelting_Progress import *

x = Cloth()
y = Bronze_Ingot()

y.smelted(x,y)

尝试运行后,我收到此错误:

Traceback (most recent call last):
File "C:\Users\WCS-HSSTUDENT\Desktop\Files\Project SAOffline\Coding\New Aincrad World\Items\NAI_Smelted.pyw", line 6, in <module>
    Metal.smelted(Fuel, Metal)
TypeError: 'tuple' object is not callable

【问题讨论】:

  • 无关:您忘记在 __init__ 函数中调用父构造函数。
  • 请注意,您的回溯与您发布的代码不匹配

标签: python object tuples typeerror callable


【解决方案1】:

你有一个实例属性smelted;你把它设置在Metal.__init__():

self.smelted = smelted

您的 Bronze_Ingot 子类将其设置为一个空元组:

self.smelted = ()            

您不能同时让方法和元组使用相同的名称。重命名一个或另一个。

如果您打算将smelted() 代码用作函数,则在顶层定义它(与您的类相同的缩进),并将其作为函数而不是方法调用:

smelted(x, y)

(注意,前面没有y.)。

【讨论】:

  • @Clanton:您正在调用y.smelted,这是元组。如果smelted(Fuel, Metal) 是一个函数,那么它不是您的某个类的方法,不应在实例上调用。删除y.
  • 好的,这已经完成了,但是我收到错误: Traceback (last recent call last): File "C:\Users\WCS-HSSTUDENT\Desktop\Files\Project SAOffline\Coding \New Aincrad World\Items\NAI_Smelted.pyw", line 6, in smelted(x, y) NameError: name 'smelted' is not defined
  • @Clanton:我确实提到您需要将def smelted() 放在顶层,而不是像类上的方法那样缩进。 :-)
  • 现在我没有语法错误,但我没有打印出想要的短语。
  • @Clanton:自己做一点调试;看看你的 fuel_timesmelt_time 值.. 你认为 5 - 15 会大于 0 吗?
猜你喜欢
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多