在python种实现类的静态方法
来源:互联网 发布:游龙网络 编辑:程序博客网 时间:2024/06/02 05:15
题目:在python种实现类的静态方法
摘要:讲述在python种实现类似java的static method的方法
时间:2005年02月24日
方法一:使用__call__
eg:
import os
class Command:
class _StaticCommand:
def __call__(self,command):
os.system(command)
run = _StaticCommand()
if __name__ == '__main__':
Command.run("dir /w") #windows
Command.run("ls -l") #unix linux
实现原理:定义一个内部类(_StaticCommand),外层类(Command)有一个类属性run指向_StaticCommand的实例,关键在于定义
_StaticCommand的特殊方法__call__,这个方法当_StaticCommand的实例使用()时刻回调,(比如:_StaticCommand()()),
就好像实例是一个方法一样
方法二:使用python2.2以后提供的staticmethod类
import os
class Command:
def run(command):
os.system(command)
run = staticmethod(run) #staticmethod接受构造参数函数引用,此时run成为staticmethod类实例
if __name__ == '__main__':
Command.run("dir /w") #windows
Command.run("ls -l") #unix linux
- 在python种实现类的静态方法
- Python中静态方法的实现
- python 的静态方法和静态类
- python类静态变量实现方法
- python的类方法和静态方法
- python的类方法和静态方法
- python的静态方法
- Python 静态类、静态方法
- Python类里的静态方法函数
- Python类的静态方法解析
- Python使用函数默认值实现函数静态变量的方法
- Python 类的静态字段和静态方法
- Python中类的静态函数、静态变量的实现
- Python的类方法,静态方法,实例方法的区别
- Python的类方法,静态方法,实例方法的区别
- python 类的实例方法,静态方法,类方法辨析
- Python类的成员方法、类方法与静态方法
- Python 静态方法 类方法
- C++字符串完全指南(2)
- 面向服务架构(SOA)带给我们什么?(转)
- 1.今天终于有了自己的Blog!
- subversion(1)
- CSS
- 在python种实现类的静态方法
- 题
- 各种包的解压方法
- 休养的重点不在「休」,而在「养」
- USB有关的编程
- 运行命令集
- Tripwire档案比对工具
- 一些常用的辅助代码 (网络收藏)
- 谈谈Jesse James Garrett提到的Ajax