程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python magic method (6):__ str__ (self) method

編輯:Python

Python There are some magical ways to do it , They are always surrounded by double underscores , They are object-oriented Of Python Everything . They are special ways to add magic to your classes , If your object implements ( heavy load ) A magic method , Then this method will be automatically used in special cases Python The call .

function

Definition object is str() The behavior of a function call , Generally for program users .

Parameters

self Represents the object itself .

Return value

Must be a string , Otherwise, throw an exception .

Example

class Point:
def __init__(self, x, y):
self.x = x
self.y = y
if __name__ == "__main__":
p = Point(3, 4)
print(p)

Execution results :

<__main__.Point object at 0x000001FD666F7B50>

This code defines a simple class , It has x and y Two elements , But if you run it directly , An address will be output on the screen . The result is some information about the instance when the interpreter executes , But it has little reference value , What we want is the specific value in this instance , Instead of an address in memory .

To achieve this function , There are many ways , One of them is __str__.

__str__ The method is similar to Java In the middle of toString Method , You can return the result after the instance is converted into a string as needed .

such as , This method can be overloaded in the class , You can output the results as needed :

class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f'x: {self.x}, y: {self.y}'
if __name__ == "__main__":
p = Point(3, 4)
print(p)

Execution results :

x: 3, y: 4

Let's take another look at the following example :

class MyText:
name = 1
# def __repr__(self) -> str:
# return ' Uh huh '
def __str__(self):
return 'My is str'
sample = MyText()
print(sample) # My is str
print(str(sample)) # My is str

Execution results :

My is str
My is str

__repr__ and __str__ Summary of methods :

1、 If __repr__ and __str__ Are not defined , that print( object ),print(str( object )),print(repr( object )) Will output the address of the object .
2、 If only __repr__ Defined , So then print( object ),print(str( object )),print(repr( object )) Will output the return value of this method .
3、 If only __str__ Defined , that print( object ) and print(str( object )) The return value of this method will be output ,print(repr( object )) The address of the object will be output .
4、 If both are defined , that print( object ) Will be output __str__ Return value of method ,print(str( object )),print(repr( object )) The return value of each method will be output .
Simply speaking ,__str__ Can be __repr__ Instead of (__str__ When there is no definition ), Not vice versa , but __str__ Higher priority .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved