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插件编写基础之“又见弹窗”
本文将通过一个实例来引出jQuery插件开发中的一些细节,首先介绍下jQuery插件开发的一些基础知识. jQuery的插件开发主要分为两类: 1. 类级别,即在jQuery类本身上扩展方法,类似与 ...
- async/await工作机制探究--NodeJS
ES6中的async/await让Promise变得更加简便,通常await处理的链式Promise会包裹在函数中,返回结果仍然是一个Promise对象. 但是当await直接处理链式Promise时 ...
- 41F继电器座的解剖与妙用
摘要:如果继电器不是焊在电路板上使用,就需要有个插座,这样方便接线,否则继电器的管脚是没法固定导线的.实际项目中使用了HF41F的继电器(宏发),在选择继电器座的时候,有一点感想,分享给大家.继电器是 ...
- C++默认成员函数
1.什么是面向对象? 概念:(Object Oriented Programming,缩写:OOP)是一种程序设计范型,同时也是一种程序开发的方法. 对象指的是类的实例,将对象作为程序的基本单元,将程 ...
- 一个IT男的表白
致BCD6 CEC0 C3F4 转一轮肩胛骨 倒一杯铁观音 白驹过隙,倏忽两秋 远方有希望和梦想 有火车.微信美颜视频聊天和碧根果 有你的支持 如果身旁没有你 生活无趣失去动力 就像python失去类 ...
- 回顾下TCP/IP协议
首先要知道什么是TCP/IP协议,从字面意思来看TCP是“Transmission Control Protocol”的缩写,也就是传输控制协议.IP是“Internet Protocol”的缩写,即 ...
- Maven ResourceBundle.getBundle读取Properties异常MissingResourceException: Can't find bundlei解决方法
参考:https://blog.csdn.net/thousa_ho/article/details/72817616 问题描述 ResourceBundle读取properties配置文件提示 Mi ...
- php从入门到放弃系列-02.php基础语法
php从入门到放弃系列-02.php基础语法 一.学习语法,从hello world开始 PHP(全称:PHP:Hypertext Preprocessor,即"PHP:超文本预处理器&qu ...
- 【异常检测】Isolation forest 的spark 分布式实现
1.算法简介 算法的原始论文 http://cs.nju.edu.cn/zhouzh/zhouzh.files/publication/icdm08b.pdf .python的sklearn中已经实现 ...
- Win7-64位PowerDesigner下MySQLODBC驱动问题
操作系统:win7-64位,PowerDesigner15.1(以下简称PD), MYSQL-ODBC-64驱动.安装完MYSQL-ODBC-64却找不到相关驱动,用PD反导数据库,却找不到Mysql ...