1. 程式人生 > >python 幾個常用的內置函數

python 幾個常用的內置函數

python 幾個常用的內置函數

__init__

__new__

__repr__

__str__


這幾個函數的優先級分別是

new,str,repr,init

看下面的例子:

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

class A:
	def __str__(self):
		return "this is str"
	def __init__(self,value="hello world !"):
		self.value = value
	def __new__(self):
		return "this is new"
	def __repr__(self):
		return "this is repr"

a=A()

print(a)

由上面的例子可以看出他們的出場順序

python 幾個常用的內置函數