您好,登錄后才能下訂單哦!
@classmethod是給類使用的,即綁定到類,
1.類在調用該方法時,會將類本身當做參數傳給類方法的第一個參數(即self)
2.對象在調用該方法時,也會將類本身當做參數傳給類方法的第一個參數(即self)
3.哪個類調用,就傳入哪個類,子類在調用父類中的類綁定方法時,傳入的是子類
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class MySQL:
def __init__(self,host,port):
self.host=host
self.port=port
@classmethod
def from_conf(cls):
print(cls)
conn=MySQL("127.0.0.1","3306")
print(MySQL.from_conf)
print(conn.from_conf)
#對象也可以調用,但是默認傳的第一個參數仍然是類
MySQL.from_conf()
conn.from_conf()
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
<bound method MySQL.from_conf of <class '__main__.MySQL'>>
<bound method MySQL.from_conf of <class '__main__.MySQL'>>
<class '__main__.MySQL'>
<class '__main__.MySQL'>
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class MySQL:
def __init__(self,host,port):
self.host=host
self.port=port
@classmethod
def from_conf(cls):
print(cls)
class mysql(MySQL):
pass
# 哪個類調用,傳入的就是哪個類的。這里傳入的是子類mysql的類
mysql.from_conf()
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
<class '__main__.mysql'>
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class MySQL:
def __init__(self,host,port):
self.host=host
self.port=port
@classmethod
def from_conf(cls):
print(cls)
# 綁定到對象的方法
def from_obj(cls):
print(cls)
obj = MySQL("","")
print(obj.from_conf)
# 綁定到對象的方法
print(obj.from_obj)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
<bound method MySQL.from_conf of <class '__main__.MySQL'>>
<bound method MySQL.from_obj of <__main__.MySQL object at 0x0000027E6AD9EB00>>
Process finished with exit code 0
在類內部使用staticmethod裝飾的函數即是非綁定方法,就是普通函數
staticmethod不與類或對象綁定,誰都可以調用,沒有自動傳值的效果
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class MySQL:
def __init__(self,host,port):
self.host=host
self.port=port
@staticmethod
def from_conf(cls):
print(cls)
#return cls("127.0.0.1","3306")
conn=MySQL("127.0.0.1","3306")
MySQL.from_conf("class test")
conn.from_conf("obj test")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
class test
obj test
Process finished with exit code 0
"非綁定方法也可以是隱藏函數,同樣是不能被外部調用"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class MySQL:
def __init__(self,host,port):
self.host=host
self.port=port
@staticmethod
def __from_conf(cls):
print(cls)
#return cls("127.0.0.1","3306")
conn=MySQL("127.0.0.1","3306")
# "兩條語句報錯都是下面的錯誤,可以自行嘗試"
MySQL.from_conf("class test")
conn.from_conf("obj test")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
File "E:/PythonProject/python-test/BasicGrammer/test.py", line 19, in <module>
conn.from_conf("obj test")
AttributeError: 'MySQL' object has no attribute 'from_conf'
Process finished with exit code 1
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。