python classmethod 和 staticmethod的区别
https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner
1.classmethod还可以调用内部的classmethod和staticmethod(是的可以调用staticmethod,cls.staticmethod)。
2.staticmethod不能调用内部的任何方法。
3.classmethod第一个参数cls表示这个类,staticmethod不需要这个参数,staticmethod只是一个函数,不能调用类内部的其它方法(可以被调用)。
#!/usr/bin/python
# -*- coding: utf-8 -*- class DateProcessor(object):
def __init__(self):
pass def parse_date(self, date_str):
print date_str
self.print_date(date_str) # 调用了calssmethod
self.print_date_1(date_str) # 调用了staticmethod @classmethod
def print_date(cls, date_str):
cls.print_date_2(date_str) # classmethod可以调用类里面其它classmethod
cls.print_date_1(date_str) # 也可以调用staticmethod
print date_str @classmethod
def print_date_2(cls, date_str):
# 可以被其它方法调用
print "date_str_2:", date_str @staticmethod
def print_date_1(date_str):
# 可以被其它方法调用,但是不能调用其它方法
print "date_str_1:", date_str if __name__ == "__main__":
date_process = DateProcessor()
date_process.parse_date("2017-06-29") # 实例化 DateProcessor.print_date("2017-06-30") # 不用实例化类,调用classmethod
DateProcessor.print_date_1("2017-06-30") # 不用实例化类,调用staticmethod
python classmethod 和 staticmethod的区别的更多相关文章
- python @classmethod和@staticmethod区别
python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...
- Fluent Python: Classmethod vs Staticmethod
Fluent Python一书9.4节比较了 Classmethod 和 Staticmethod 两个装饰器的区别: 给出的结论是一个非常有用(Classmethod), 一个不太有用(Static ...
- Python @classmethod和@staticmethod装饰器使用介绍
@classmethod和@staticmethod装饰器使用介绍 by:授客 QQ:1033553122 简介 静态方法:类中用 @staticmethod装饰的不带 self 参数的方法.类的静态 ...
- Python中classmethod和staticmethod的区别
学习python中经常会出现一些相近或者相似的语法模块等,需要对比分析才能加深记忆,熟练运用. staticmethod:静态方法 classmethod:类方法 在python中,静态方法和类方法都 ...
- python 3全栈开发-面向对象之绑定方法(classmethod与staticmethod的区别)、多态、封装的特性property
一.面向对象绑定方法 一.类中定义的函数分成两大类 1.绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入): 1. 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 ...
- Python中的实例方法、classmethod和staticmethod的区别
class NewsPaper(object): # 类属性 __print_times = 0 # 下划线表示私有属性 # 实例方法 def __init__(self, title, conten ...
- python3 @classmethod 和 @staticmethod 的区别
如果您将某个东西定义为classmethod,这可能是因为您打算从类而不是类实例中调用它. 定义类方法的几种方式: 常规方式 : 需要self隐士传递当前类 ...
- 洗礼灵魂,修炼python(47)--巩固篇—定义类的方法之@classmethod,@staticmethod
定义类的方法,相信你会说,不就是在class语句下使用def () 就是定义类的方法了嘛,是的,这是定义的方法的一种,而且是最普通的方式 首先,我们已经知道有两种方式: 1.普通方法: 1)与类无关的 ...
- python -- @classmethod @staticmethod区别和使用
python中的定义: class MyClass: ... @classmethod # classmethod的修饰符 def class_method(cls, arg1, arg2, ... ...
随机推荐
- 第一章 spring起步
点击 网址 http://start.spring.io/ 就可以获得spring-boot的项目结构. 如下: 将项目解压到自己的项目中,然后找到mian函数所在启动类.运行.出现: 表示已经运行了 ...
- Windows OS系统变量
%userprofile% C:\Users\Administrator\ %windir% C:\Windows\
- GIS(地理信息系统)
ylbtech-杂项:GIS(地理信息系统) 地理信息系统(Geographic Information System或 Geo-Information system,GIS)有时又称为“地学信息系统 ...
- stm32串口接收完整的数据包
参考了文章:<stm32串口中断接收方式详细比较> 文章地址:http://bbs.elecfans.com/jishu_357017_1_1.html 借鉴了第四种中断方式 串口的配置这 ...
- 【C++11新特性】 - 空间配置allocator类
原文链接: http://blog.csdn.net/Xiejingfa/article/details/50955295 今天我们来讲讲C++的allocator类. C++提供了new和delet ...
- ORACLE中index的rebuild(转)
Oracle里大量删除记录后,表和索引里占用的数据块空间并没有释放. table move可以释放已删除记录表占用的数据块空间,整理碎片.如果将表格用move方式整理碎片后,索引将失效,这时需要将索引 ...
- JAVA中关于set()和get()方法的理解及使用
对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...
- 通过yum升级gcc/g++至版本4.8.2
最近在坐一个日期处理的问题,需要安装sxtwl模块,但是gcc版本4.7死活也安装不上,最后测试到只能升级gcc版本到4.8才得以解决 [root@123 bin]# gcc -vUsing buil ...
- tensorboard-sklearn数据-loss
记录sklearn数据训练时的loss值,用tensorboard可视化 三步骤:红字处 import tensorflow as tf from sklearn.datasets import lo ...
- linux下创建django-app
Django 1.创建一个项目linux :django-admin startproject helloword 创建项目helloword2.开始一个工程 manage.py 文件 它是djang ...