重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

绑定方法与非绑定方法

1.绑定方法

1.1绑定到类的方法:@classmethod装饰器装饰的方法

@classmethod是给类使用的,即绑定到类,
1.类在调用该方法时,会将类本身当做参数传给类方法的第一个参数(即self)
2.对象在调用该方法时,也会将类本身当做参数传给类方法的第一个参数(即self)
3.哪个类调用,就传入哪个类,子类在调用父类中的类绑定方法时,传入的是子类

创新互联公司专注于企业成都全网营销推广、网站重做改版、禹会网站定制设计、自适应品牌网站建设、H5页面制作成都商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为禹会等各大城市提供网站开发制作服务。

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



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


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

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 
    conn.from_conf("obj test")
AttributeError: 'MySQL' object has no attribute 'from_conf'

Process finished with exit code 1

当前名称:绑定方法与非绑定方法
本文链接:http://cqcxhl.cn/article/jopssd.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP