Python-TypeError:“ int”对象不可调用 [英] Python - TypeError: 'int' object is not callable

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

问题描述

(使用Python 2.7)

(Using Python 2.7)

您好,

我有两个版本的PairOfDice。

I've two version of a class PairOfDice.

1。)这不起作用,并引发错误。

1.) This one is not working and throws an error.


TypeError:'int'对象不可调用

TypeError: 'int' object is not callable



import random

class PairOfDice:
    """ Represent the Pair of Dices and have method which tells the total of those roles.
    """
    def roll(self):
        self.total = random.randint(1, 6) + random.randint(1, 6)

    def total(self):
        return self.total

    def name(self, name):
        self.name = name

    def getName(self):
        return self.name

player1 = PairOfDice()
player1.roll()
print player1.total()

2)这个正在工作。

import random

class PairOfDice:
    """ Represent the Pair of Dices and have method which tells the  total of those roles.
    """
    def roll(self):
        self.roll1 = random.randint(1, 6)
        self.roll2 = random.randint(1, 6)

    def total(self):
        return self.roll1 + self.roll2

    def name(self, name):
        self.name = name

    def getName(self):
        return self.name

player1 = PairOfDice()
player1.roll()
print player1.total()

请问有人可以解释第一个问题吗?

Can please someone explain what's wrong with the first one?

谢谢

推荐答案

在第一类中,总计 是该类的函数和属性。那不行:) Python认为您在最后一行中引用的总数是整数变量total而不是函数。

In the first class total is a function as well as an attribute of the class. That is not okay :) Python thinks that the total you are referring to in the final line is the integer variable total and not the function.

这被认为是一种好习惯将函数 total 命名为 get_total 代替

It is considered a good practice to name the function total as get_total instead

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

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