亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

綁定方法與非綁定方法

發布時間:2020-09-24 19:07:47 來源:網絡 閱讀:333 作者:DevOperater 欄目:編程語言

1.綁定方法

1.1綁定到類的方法:@classmethod裝飾器裝飾的方法

@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

1.2綁定到對象的方法:沒有被任何裝飾器裝飾的方法

#!/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

2.非綁定方法

在類內部使用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
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

萨嘎县| 阜康市| 姜堰市| 革吉县| 南康市| 手游| 龙里县| 桂阳县| 南岸区| 浠水县| 广德县| 大方县| 浦江县| 定远县| 本溪| 巴马| 乾安县| 汾阳市| 鱼台县| 溧水县| 永泰县| 海口市| 德昌县| 临洮县| 汉源县| 哈密市| 唐山市| 紫云| 平远县| 田东县| 赣榆县| 沙河市| 奎屯市| 昂仁县| 辽中县| 呼图壁县| 渝中区| 清远市| 南投县| 江陵县| 衡山县|