Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)
原文地址:http://blog.csdn.net/caroline_wendy/article/details/23383995
还有一篇:http://blog.csdn.net/carolzhang8406/article/details/6856817
静态函数(staticmethod), 类函数(classmethod), 成员函数的区别(完全解析)
定义:
静态函数(@staticmethod): 即静态方法,主要处理与这个类的逻辑关联, 如验证数据;
类函数(@classmethod):即类方法, 更关注于从类中调用方法, 而不是在实例中调用方法, 如构造重载;
成员函数: 实例的方法, 只能通过实例进行调用;
代码:
# -*- coding: utf-8 -*- #eclipse pydev, python 3.3
#by C.L.Wang class A(object): _g = 1 def foo(self,x):
print('executing foo(%s,%s)'%(self,x)) @classmethod
def class_foo(cls,x):
print('executing class_foo(%s,%s)'%(cls,x)) @staticmethod
def static_foo(x):
print('executing static_foo(%s)'%x) a = A()
a.foo(1)
a.class_foo(1)
A.class_foo(1)
a.static_foo(1)
A.static_foo('hi')
print(a.foo)
print(a.class_foo)
print(a.static_foo)
输出:
具体应用:
比如日期的方法, 可以通过实例化(__init__)进行数据输出;
可以通过类方法(@classmethod)进行数据转换;
可以通过静态方法(@staticmethod)进行数据验证;
代码:
输出:
12*11*2014
11*13*2014
False
False
参考:
http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner
http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python
Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)的更多相关文章
- 面试题:python 中 staticmethod 和 classmethod有什么区别
面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...
- python 中 staticmethod 和 classmethod有什么区别
面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...
- python(三)@staticmethod和@classmethod使用和区别
转载自[1] 一般要用某个类的方法,先要实例这个类. 但是可以通过@staticmethod和@classmethod,直接用“类.方法()”来调用这个方法. 而 @staticmethod和@cla ...
- 基于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的作用与区别
前言 Python其实有3个方法,即静态方法(staticmethod),类方法(classmethod)和实例方法,一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法.而使用@static ...
- python中 staticmethod与classmethod区别
staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...
- python中@staticmethod与@classmethod
@ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...
- python 之@staticmethod和@classmethod
在python中,要调用一个类中的方法,一般的操作步骤如下: 1.实例化此类 2.调用此类中的方法 而@staticmethod和@classmethod则打破了这种引用方式,可以在不实例化类的情况下 ...
- python中 staticmethod与classmethod
原文地址https://blog.csdn.net/youngbit007/article/details/68957848 原文地址https://blog.csdn.net/weixin_3565 ...
随机推荐
- jQuery瀑布流详解(PC及移动端)
前言 瀑布流布局已成为当今非常普遍的图片展示方式,无论是PC还是手机等移动设备上.这种布局图片的样式大概分为三种:等高等宽.等宽不等高.等高不等宽,接下来我们就最为普遍的等宽不等高形式来作为示例. 我 ...
- 利用VBS脚本实现Telnet自动连接
把以下代码保存为*.vbs文件,替换IP.用户名.密码. Dim objShell Set objShell = CreateObject("Wscript.Shell") obj ...
- 【SIKIA计划】_10_Unity5.1UI系统-UGUI笔记
Canvas——TextEventSystem 事件系统 0.滚动文本列表(隐藏背景)/Scroll/maskimage[Scroll Rect][Mask]——text(拉伸到显示全部)Scroll ...
- 深度学习中数据的augmentation
为了提高模型的泛化能力,同时也为了增大数据集,我们往往需要对数据进行augmentation,在这篇博客中,将总结一下可以对数据进行的augmentation. 1.颜色数据增强,对图像亮度.饱和度. ...
- 2017-2018-2 1723 『Java程序设计』课程 结对编程练习-四则运算-中坚阶段
2017-2018-2 1723 『Java程序设计』课程 结对编程练习-四则运算-中坚阶段 如今的编写,进入了一段时间的中坚阶段,小伙伴的思路虽然很一致,但是总是存在着一定问题,这就造成了我们中坚阶 ...
- 学习pl/sql之一
--使用pl/sql语句打印一个hello world begin dbms_output.put_line('hello,world'); end; 但是在sqlplus里面就不一样了 ...
- HTML和CSS <h1> --2-- <h1>
认识html文件基本结构 这一节中我们来学习html文件的结构:一个HTML文件是有自己固定的结构的. <html> <head>...</head> <bo ...
- iis托管管道模式-学习
文章;IIS 7 托管管道模式 经典模式(Classic) 集成模式(Integrated) 分析与理解 我们可以通过应用程序池设置管道模式,这项功能对IIS管理员尤其有用,因为这样既可以令一台服务器 ...
- 如何解决abd.exe已停止工作
打开电脑,右键点击属性会出现如下界面: 点击左边高级系统设置:将会出现如下界面: 点击环境变量,点编辑. 把环境变量中的 ANDROID_ADB_SERVER_PORT 改成1122以后还遇到这个问 ...
- Alpha版本冲刺(五)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...