Python中@staticmethod和@classmethod的作用和区别
简单介绍一下两者的区别:
对于一般的函数test(x),它跟类和类的实例没有任何关系,直接调用test(x)即可
#!/usr/bin/python
# -*- coding:utf-8 -*- def foo(x):
print "running (%s)" % x foo("test")
对于普通的类,来调类中的函数:
#!/usr/bin/python
# -*- coding:utf-8 -*- class A:
def test(self, x):
print "running (%s, %s)" % (self, x) a = A()
a.test("test")
当我们需要和类直接进行交互,而不需要和实例进行交互时,类方法是最好的选择。
类方法与实例方法类似,但是传递的不是类的实例,而是类本身,第一个参数是cls。我们可以用类的实例调用类方法,也可以直接用类名来调用。
#!/usr/bin/python
# -*- coding:utf-8 -*- class A:
class_attr = "test" def __init__(self):
pass @classmethod
def class_test(cls):
print "running class_test(%s)" % (cls.class_attr) a = A()
a.class_test()
A.class_test()
静态方法类似普通方法,参数里面不用self。这些方法和类相关,但是又不需要类和实例中的任何信息、属性等等。
如果把这些方法写到类外面,这样就把和类相关的代码分散到类外,使得之后对于代码的理解和维护都是巨大的障碍。而静态方法就是用来解决这一类问题的。
比如我们检查是否开启了日志功能,这个和类相关,但是跟类的属性和实例都没有关系。
#!/usr/bin/python
# -*- coding:utf-8 -*- log_enabled = True class A:
class_attr = "attr" def __init__(self):
pass @staticmethod
def static_test():
if log_enabled:
print("log is enabled")
else:
print("log is disabled") A.static_test()
Python中@staticmethod和@classmethod的作用和区别的更多相关文章
- 【Python】@staticmethod和@classmethod的作用与区别
前言 Python其实有3个方法,即静态方法(staticmethod),类方法(classmethod)和实例方法,一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法.而使用@static ...
- 基于python中staticmethod和classmethod的区别(详解)
例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object): def foo(self,x): print "executing foo ...
- python中@staticmethod与@classmethod
@ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...
- 面试题:python 中 staticmethod 和 classmethod有什么区别
面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...
- python 中 staticmethod 和 classmethod有什么区别
面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...
- python中@staticmethod、@classmethod和实例方法
1.形式上的异同点: 在形式上,Python中:实例方法必须有self,类方法用@classmethod装饰必须有cls,静态方法用@staticmethod装饰不必加cls或self,如下代码所示: ...
- python中 staticmethod与classmethod区别
staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...
- python中 staticmethod与classmethod
原文地址https://blog.csdn.net/youngbit007/article/details/68957848 原文地址https://blog.csdn.net/weixin_3565 ...
- 飘逸的python - @staticmethod和@classmethod的作用与区别
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...
随机推荐
- 泡泡一分钟:eRTIS - A Fully Embedded Real Time 3D Imaging Sonar Sensor for Robotic Applications
eRTIS - A Fully Embedded Real Time 3D Imaging Sonar Sensor for Robotic Applications eRTIS - 用于机器人应用 ...
- 全面系统Python3入门+进阶-1-5 一个经典误区
结束
- Python - Django - ORM QuerySet 方法补充
models.py: from django.db import models class Employee2(models.Model): name = models.CharField(max_l ...
- 实现不同的项目,用不同的git 账号提交
可以全局配置一个git 账户名和密码,然后在具体项目里单独配置一个账户名和密码 例如: git config --global user.name "winyh" git conf ...
- Swift4.0复习特性、编译标志和检查API的可用性
1.Swift中的特性: @引出,后面紧跟特性名,圆括号带参数即可. @attribute(args) avaiable: 指明对象,函数,类型的可用性. @available(iOS 10.0, m ...
- LODOP带空格和不带空格的字体对齐
有时候需要用到字体上下对齐,有些需要的文字较多,较少的文字需要加部分空格才能向上面的文字对齐.本文实际测试了一下字体对齐需要的空格.代码是在editplus里写的,该编辑软件里的字体首选项设置的是Co ...
- 删除SQL约束的方法
在SQL数据库中,如果需要删除表约束,应该如何操作呢?下面就将为您介绍删除SQL表约束的方法,供您参考,希望对您有所帮助. --1)禁止所有表约束的SQL select 'alter table '+ ...
- 基于vue-cli、elementUI的Vue简单入门例子
vue-cli.elementUI的安装教程请看: https://www.cnblogs.com/joe235/p/12013818.html 把HelloWorld.vue文件修改为: <t ...
- mysql 基本操作及对用户操作
1.登录/退出基本操作 登录:mysql [-h服务器地址] -u登录名 -P端口号 -p 或登录:mysql [--host=服务器地址] --user ...
- Windows 下配置 ApacheBench (AB) 压力测试
下载 http://httpd.apache.org/download.cgi 我用的是ApacheHaus. 安装服务 1. 打开apache目录下的 conf/httpd.conf,搜索并修改 L ...