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...

猜你喜欢

转载自www.cnblogs.com/neozheng/p/12289638.html
今日推荐