TypeError:'元组'尝试调用方法时,对象不可调用

时间:2015-01-13 16:41:33

标签: python object tuples typeerror callable

以下是我到目前为止所做的事情:

# -*- 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

1 个答案:

答案 0 :(得分:5)

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

self.smelted = smelted

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

self.smelted = ()            

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

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

smelted(x, y)

(注意,前面没有y.