Python 打印自定义对象
在 Python 中,我们可以自定义对象,并对其进行打印输出。当我们定义的对象中包含了一些特殊方法(比如__str__
、__repr__
等),我们可以通过调用这些特殊方法来控制对象的打印输出内容和格式。本文将详细介绍如何在 Python 中打印自定义对象,并给出一些示例代码。
使用__str__
方法打印对象
__str__
方法是一个特殊方法,用于返回对象的字符串表示形式。在打印对象时,Python 会调用str()
函数,并在其中调用对象的__str__
方法。我们可以在自定义对象中重写__str__
方法,以控制对象的打印输出内容。
下面是一个简单的示例,展示了如何定义一个自定义对象,并在其中重写__str__
方法:
class GeekDocs:
def __init__(self, content):
self.content = content
def __str__(self):
return f"GeekDocs content: {self.content}"
geek_docs = GeekDocs("Welcome to geek-docs.com")
print(geek_docs)
运行结果:
GeekDocs content: Welcome to geek-docs.com
在上面的示例中,我们定义了一个GeekDocs
类,其中包含一个名为content
的属性。我们在__str__
方法中返回了一个以"GeekDocs content:"
开头的字符串,后面跟上对象的content
属性的值。当我们打印geek_docs
对象时,Python会调用它的__str__
方法,从而输出了我们定义的字符串表示形式。
使用__repr__
方法打印对象
除了__str__
方法外,Python还提供了另一个特殊方法__repr__
,用于返回对象的“官方”字符串表示形式。在打印对象时,如果对象没有__str__
方法,则Python会调用其__repr__
方法。如果对象同时实现了__str__
和__repr__
方法,__str__
方法优先于__repr__
方法。
下面是一个示例代码,展示了如何使用__repr__
方法打印对象:
class GeekDocs:
def __init__(self, content):
self.content = content
def __repr__(self):
return f"GeekDocs('{self.content}')"
geek_docs = GeekDocs("Welcome to geek-docs.com")
print(repr(geek_docs))
运行结果:
GeekDocs('Welcome to geek-docs.com')
在上面的示例中,我们定义了一个GeekDocs
类,并在其中重写了__repr__
方法。该方法返回一个以"GeekDocs('"
开头的字符串,后面跟上对象的content
属性的值,再后面跟上"')"
。与__str__
不同的是,我们在调用repr()
函数时显式地调用了GeekDocs
对象的__repr__
方法。
控制对象的打印输出格式
除了重写__str__
和__repr__
方法外,我们还可以进一步控制对象的打印输出格式。下面是一个示例代码,展示了如何通过自定义__format__
方法来控制打印输出格式:
class GeekDocs:
def __init__(self, content):
self.content = content
def __str__(self):
return f"GeekDocs content: {self.content}"
def __format__(self, format_spec):
if format_spec == 'upper':
return self.content.upper()
elif format_spec == 'lower':
return self.content.lower()
else:
return self.content
geek_docs = GeekDocs("Welcome to geek-docs.com")
print("Upper case:", format(geek_docs, 'upper'))
print("Lower case:", format(geek_docs, 'lower'))
print("Default case:", geek_docs)
运行结果:
Upper case: WELCOME TO GEEK-DOCS.COM
Lower case: welcome to geek-docs.com
Default case: GeekDocs content: Welcome to geek-docs.com
在上面的示例中,我们定义了一个GeekDocs
类,并在其中重写了__format__
方法。该方法根据format_spec
参数来判断用户想要的格式,并返回相应的内容。我们分别以'upper'
和'lower'
格式打印了geek_docs
对象的内容,并以默认格式打印了对象。
自定义对象的打印输出
我们还可以通过自定义对象的__format__
方法,进一步控制对象的打印输出内容。下面是一个示例代码,展示了如何在自定义对象中实现自定义的打印输出:
class GeekDocs:
def __init__(self, content):
self.content = content
def __str__(self):
return f"GeekDocs content: {self.content}"
def __format__(self, format_spec):
if format_spec == 'url':
return f"Visit us at http://geek-docs.com"
else:
return self.content
geek_docs = GeekDocs("Welcome to geek-docs.com")
print("URL:", format(geek_docs, 'url'))
print("Default case:", geek_docs)
运行结果:
URL: Visit us at http://geek-docs.com
Default case: GeekDocs content: Welcome to geek-docs.com
在上面的示例中,我们在__format__
方法中定义了一个'url'
格式,使得对象在以'url'
格式打印时会返回一个类似网址的字符串。当以默认格式打印对象时,仍然输出对象的默认内容。
通过以上示例,我们可以看到如何在 Python 中打印自定义对象,并控制对象的打印输出内容和格式。通过重写__str__
、__repr__
和__format__
等特殊方法,我们可以根据需求定制对象的打印输出,使其更加符合我们的期望。