Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)
来源:互联网 发布:如何评价朱元璋 知乎 编辑:程序博客网 时间:2024/06/14 19:18
静态函数(staticmethod), 类函数(classmethod), 成员函数的区别(完全解析)
版权所有, 禁止转载, 如有需要, 请站内联系.
本文地址: http://blog.csdn.net/caroline_wendy/article/details/23383995
定义:
静态函数(@staticmethod): 即静态方法,主要处理与这个类的逻辑关联, 如验证数据;
类函数(@classmethod):即类方法, 更关注于从类中调用方法, 而不是在实例中调用方法, 如构造重载;
成员函数: 实例的方法, 只能通过实例进行调用;
代码:
# -*- coding: utf-8 -*-#eclipse pydev, python 3.3#by C.L.Wangclass 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)
输出:
executing foo(<__main__.A object at 0x01E2E1B0>,1)executing class_foo(<class '__main__.A'>,1)executing class_foo(<class '__main__.A'>,1)executing static_foo(1)executing static_foo(hi)<bound method A.foo of <__main__.A object at 0x01E2E1B0>><bound method type.class_foo of <class '__main__.A'>><function A.static_foo at 0x01E7E618>
具体应用:
比如日期的方法, 可以通过实例化(__init__)进行数据输出;
可以通过类方法(@classmethod)进行数据转换;
可以通过静态方法(@staticmethod)进行数据验证;代码:
# -*- coding: utf-8 -*-#eclipse pydev, python 3.3#by C.L.Wangclass Date(object): day = 0 month = 0 year = 0 def __init__(self, day=0, month=0, year=0): self.day = day self.month = month self.year = year def display(self): return "{0}*{1}*{2}".format(self.day, self.month, self.year) @classmethod def from_string(cls, date_as_string): day, month, year = map(int, date_as_string.split('-')) date1 = cls(day, month, year) return date1 @staticmethod def is_date_valid(date_as_string): day, month, year = map(int, date_as_string.split('-')) return day <= 31 and month <= 12 and year <= 3999 date1 = Date('12', '11', '2014')date2 = Date.from_string('11-13-2014')print(date1.display())print(date2.display())print(date2.is_date_valid('11-13-2014'))print(Date.is_date_valid('11-13-2014'))
输出:
12*11*201411*13*2014FalseFalse
参考:
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
2 0
- Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)
- Python: classmethod类函数 & staticmethod静态函数 区别
- python中staticmethod classmethod及普通函数的区别
- python中staticmethod classmethod及普通函数的区别
- python中staticmethod classmethod及普通函数的区别
- classmethod和staticmethod的函数区别
- python之静态和类方法:staticmethod和classmethod内置函数
- Python内置函数的staticmethod()与classmethod(),@staticmethod,@classmethod
- Python: classmethod & staticmethod 区别
- Python: classmethod & staticmethod 区别
- Python: classmethod & staticmethod 区别
- python @staticmethod @classmethod 区别
- 静态成员函数解析
- python的静态方法以及@staticmethod或@classmethod的区别
- Python @classmethod和@staticmethod区别
- python中@classmethod @staticmethod区别
- staticmethod classmethod 创建静态类
- python 静态方法staticmethod和类方法classmethod
- 利用flandmark进行face alignment
- Win Server 2008中开启Areo特效
- ListView异步加载优化
- Windows 7上可以上QQ,无法访问网页的DNS解析的解决
- malloc和new有什么区别
- Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)
- ZOJ 3605 Find the Marble(DP)
- 网络命令详解
- 二叉查找(排序)树——完
- Python 基础2
- Java排序算法4:希尔排序
- ThinkPHP 数据分页 带查询条件
- 数据库原理 知识点总结
- 穷举所有的双色球