[基础] - Python中 @staticmethod 和 @classmethod
2015-11-27 15:43
791 查看
[基础] - Python中 @staticmethod 和 @classmethod
staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象来调用而已, 不会隐式地传入任何参数。这个和静态语言中的静态方法比较像。classmethod 是和一个class相关的方法,可以通过类或类实例调用,并将该class对象隐式地当作第一个参数传入。类方法的第一个参数是cls,而实例方法的第一个参数是self。
class Person: def __init__(self): print "init" @staticmethod def sayHello(hello): print "staticmethod" @classmethod def introduce(cls,hello): cls.sayHello(hello) print "classmethod"
相关文章推荐
- windows 64bit selenium+python环境搭建
- Python 文件操作
- pygame for python3.5
- Python学习笔记(1)
- Python 文本挖掘:使用gensim进行文本相似度计算
- Python flask多维字典实现菜单动态添加
- Python cPickle模块
- 一些有趣的python开源项目
- Some Notes of Python Interfaces Pycaffe (Caffe)
- python 跳板机登录脚本
- Python安装配置
- 如何用Python写一个贪吃蛇AI
- python 猜数字游戏
- Python ORM Storm 源码修改
- python 用户交互
- 2015.11.27-------KNN算法的python实现的应用
- LeetCode OJ 系列之63 Unique Paths II --Python
- LeetCode OJ 系列之62 Unique Paths --Python
- LeetCode OJ 系列之1 Two Sum --Python
- [基础] - Python中的@property