Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
__str__;(self)函数不适用于在Python中打印对象_Python_String_Class_Object_Printing - Fatal编程技术网

__str__;(self)函数不适用于在Python中打印对象

__str__;(self)函数不适用于在Python中打印对象,python,string,class,object,printing,Python,String,Class,Object,Printing,我一直在努力改进Python中类和对象的使用,但在程序中显示不同对象的值时遇到了问题。我的指导老师告诉我,每当对象被传递print()函数时,就会自动调用使用str(self)函数,但是,输出仍然显示对象在内存中的地址,而不是值。我在下面记录了我的代码,并在两个不同的编译程序中运行了它,但仍然找不到错误发生的地方 # the class of the bank account class BankAccount: def __init__(self, bal): self

我一直在努力改进Python中类和对象的使用,但在程序中显示不同对象的值时遇到了问题。我的指导老师告诉我,每当对象被传递print()函数时,就会自动调用使用str(self)函数,但是,输出仍然显示对象在内存中的地址,而不是值。我在下面记录了我的代码,并在两个不同的编译程序中运行了它,但仍然找不到错误发生的地方

# the class of the bank account
class BankAccount:
    def __init__(self, bal):
        self.__balance = bal

# deposits the amount given by the user
def deposit(self, amount):
    self.__balance += amount

# withdraws the amount given by the user
def withdraw(self, amount):
    if(self.__balance >= amount):
        self.__balance -= amount
    else:
        print("Error: Insufficient funds")

# returns the current balance of the user's account
def get_balance(self):
    return self.__balance

# sets the user's account given a specified balance
def set_balance(self, bal):
    self.__balance = bal

# prints the user's balance
def __str__(self):
    return "The balance is $" + format(self.__balance, ",.2f")

def main():
    start_bal = float(input("Enter your starting balance: ")) # retrieves balance from the user
    savings = BankAccount(start_bal) # creates an object that hold's the user's balance
    print(savings)

main()
程序输出的不是帐户的值,而是


请让我知道任何我可以更改以更正此问题的内容。谢谢。

您需要覆盖
BankAccount
类的
\uuuu str\uuuuu
运算符:

class BankAccount:
    def __init__(self, bal):
        self.__balance = bal

    def __str__(self):
        return "The balance is $" + format(self.__balance, ",.2f")


In [25]: start_bal = 2.3
In [27]: savings = BankAccount(start_bal) 

In [28]: print(savings)
The balance is $2.30

只需将方法正确嵌套在类中即可

这是一个缩进问题,您的类仅定义为:

# the class of the bank account
class BankAccount:
    def __init__(self, bal):
        self.__balance = bal
尝试将本节下的所有代码缩进一个独立块,直到进入
def main()
函数,这样BankAccount就可以获得您想要的方法:

# the class of the bank account
class BankAccount:
    def __init__(self, bal):
        self.__balance = bal

    # deposits the amount given by the user
    def deposit(self, amount):
        self.__balance += amount

    # withdraws the amount given by the user
    def withdraw(self, amount):
        if(self.__balance >= amount):
            self.__balance -= amount
        else:
            print("Error: Insufficient funds")

    # returns the current balance of the user's account
    def get_balance(self):
        return self.__balance

    # sets the user's account given a specified balance
    def set_balance(self, bal):
        self.__balance = bal

    # prints the user's balance
    def __str__(self):
        return "The balance is $" + format(self.__balance, ",.2f")

def main():
    start_bal = float(input("Enter your starting balance: ")) # retrieves balance from the user
    savings = BankAccount(start_bal) # creates an object that hold's the user's balance
    print(savings)

main()

当调用main并在print(savings)中执行时,它将打印BankAccount类的str表示形式,该表示形式是储蓄内存地址,并且由于str实现是在类的外部定义的,因此它与类无关

试试这个:

# the class of the bank account
class BankAccount:
    def __init__(self, bal):
        self.__balance = bal

    # deposits the amount given by the user
    def deposit(self, amount):
        self.__balance += amount

    # withdraws the amount given by the user
    def withdraw(self, amount):
        if(self.__balance >= amount):
            self.__balance -= amount
        else:
            print("Error: Insufficient funds")

    # returns the current balance of the user's account
    def get_balance(self):
        return self.__balance

    # sets the user's account given a specified balance
    def set_balance(self, bal):
        self.__balance = bal

    # prints the user's balance
    def __str__(self):
        return "The balance is $" + format(self.__balance, ",.2f")

def main():
    start_bal = float(input("Enter your starting balance: ")) # retrieves balance from the user
    savings = BankAccount(start_bal) # creates an object that hold's the user's balance
    print(savings)

main()

您的格式有问题,需要缩进定义这些函数不在类内,只有
\uuuu init\uuu
在类内,您应该正确设置代码的格式谢谢大家!不过,我有一个问题。您知道为什么函数需要在类中缩进吗?为什么即使在程序中声明了它们,我也不能正确使用它们?