且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

'int'对象不可调用错误python

更新时间:2022-06-11 04:03:54

您正在使用实例属性 balance 屏蔽您的方法余额。重命名其中一个。您可以通过使用下划线预先挂起实例属性来重命名实例属性,例如:

You are masking your method balance with a instance attribute balance. Rename one or the other. You could rename the instance attribute by pre-pending it with an underscore for example:

def set(self, bal):
    self._balance = bal

def balance(self):
    return self._balance

实例上的属性胜过类上的属性(数据描述符除外;想想属性 s)。来自类定义文档

Attributes on the instance trump those on the class (except for data descriptors; think propertys). From the Class definitions documentation:


类定义中定义的变量是类属性;它们由实例共享。可以使用 self.name = value 在方法中设置实例属性。类和实例属性都可以通过符号 self.name 访问,并且实例属性在以这种方式访问​​时会隐藏具有相同名称的类属性。

Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with self.name = value. Both class and instance attributes are accessible through the notation "self.name", and an instance attribute hides a class attribute with the same name when accessed in this way.