@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
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文标题:绑定方法与非绑定方法-创新互联
URL网址:https://www.cdcxhl.com/article4/jojoe.html
成都网站建设公司_创新互联,为您提供营销型网站建设、自适应网站、做网站、云服务器、静态网站、全网营销推广
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联