A Pythonic Object

Posted neozheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A Pythonic Object相关的知识,希望对你有一定的参考价值。

1. Object Representations

Every object-oriented language has at least one standard way of getting a string representation from any object. Python has two: repr() and str() . And the special methods __repr__ and __str__ support repr() and str().

There are two additional special methods to support alternative representations of objects: __bytes__ and __format__ . The __bytes__ is called by bytes() to get the object represented as a byte sequence . Regarding __format__ , both the built-in function format() and the str.format() method call it to get string displays of objects using special formatting codes.

# In Python3 , __repr__ , __str__ , and __format__ must always return Unicode strings (type str). Only __bytes__ is supposed to return a byte sequence (type bytes)

 

 

 

 

 

 

end...

以上是关于A Pythonic Object的主要内容,如果未能解决你的问题,请参考以下文章

使用getter和setter的pythonic方法是啥?

记录优雅的pythonic代码

#13 让代码变得Pythonic

建议2---编写pythonic代码

这段代码很Pythonic | 相见恨晚的 itertools 库

这段代码很Pythonic | 相见恨晚的 itertools 库