简体   繁体   English

Python打印对象 - TypeError:必须是str,而不是Address

[英]Python printing objects - TypeError: must be str, not Address

I have Person and Address classes. 我有PersonAddress类。 Each Person holds an Address . 每个Person持有一个Address

Code: 码:

class Address: 
    def __init__(self, street, town, city, postcode, country):
        self.street = street
        self.town = town
        self.city = city
        self.postcode = postcode
        self.country = country

    def __str__(self):
        return ("\t"   + "Street: " + self.street +
                "\n\t" + "Town: " + self.town + 
                "\n\t" + "City: " + self.city + 
                "\n\t" + "Postcode: " + self.postcode +
                "\n\t" + "Country:" + self.country)

class Person:

    def __init__(self, name, age, phone, address):
        # instance variables, unique to each Person
        self.name = name
        self.age = age
        self.phone = phone
        self.address = address

    def __str__(self):
        return "Name: " + self.name + "\n" + "Age: " + self.age + "\n" + "Phone: " + self.phone + "\n" + "Address: " + self.address

However, when I call print(p) where p is a Person , I get the following exception: 但是,当我调用print(p) ,其中pPerson ,我得到以下异常:

  File "phone2.7.py", line 30, in __str__
    return "Name: " + self.name + "\n" + "Age: " + self.age + "\n" + "Phone: " + self.phone + "\n" + "Address: " + self.address
  TypeError: must be str, not Address

Can anyone pinpoint what the issue is here? 任何人都可以找出问题所在吗? Thanks. 谢谢。

You are trying to add string and object . 您正在尝试添加stringobject It's not enough that you override the __str__ method in your Address class. 覆盖Address类中的__str__方法是不够的。 You need to call str() method. 你需要调用str()方法。

"Address: " + str(self.address)
class Address: 
def __init__(self, street, town, city, postcode, country):
    self.street = street
    self.town = town
    self.city = city
    self.postcode = postcode
    self.country = country

def __str__(self):
    return ("\t"   + "Street: " + self.street +
            "\n\t" + "Town: " + self.town + 
            "\n\t" + "City: " + self.city + 
            "\n\t" + "Postcode: " + self.postcode +
            "\n\t" + "Country:" + self.country)
class Person:
def __init__(self, name, age, phone, address):
    # instance variables, unique to each Person
    self.name = name
    self.age = age
    self.phone = phone
    self.address = address

def __str__(self):
    return "Name: " + str(self.name) + "\n" + "Age: " + str(self.age) + "\n" + "Phone: " + str(self.phone) + "\n" + "Address: " + str(self.address)

p = Person(1,2,3,4) print p p =人(1,2,3,4)打印p

Before concatenating using + , the self.address object must be converted to string using str() function. 在使用+连接之前,必须使用str()函数将self.address对象转换为字符串。

class Person:

    def __str__(self):
        return "Name: " + self.name + "\n" + "Age: " + self.age + "\n" + "Phone: " + self.phone + "\n" + "Address: " + str(self.address)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM