Python中classmethod与staticmethod区别

classmethod:类方法
staticmethod:静态方法

在python中,静态方法和类方法都是可以通过类对象和类对象实例访问。但是区别是:

      • @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法。 类方法的第一个参数cls,而实例方法的第一个参数是self,表示该类的一个实例。
      • 普通对象方法至少需要一个self参数,代表类对象实例
      • 类方法有类变量cls传入,从而可以用cls做一些相关的处理。并且有子类继承时,调用该类方法时,传入的类变量cls是子类,而非父类。 对于类方法,可以通过类来调用,就像C.f(),有点类似C++中的静态方法, 也可以通过类的一个实例来调用,就像C().f(),这里C(),写成这样之后它就是类的一个实例了。
      • 静态方法则没有,它基本上跟一个全局函数相同,一般来说用的很少
      • 如果在@staticmethod中要调用到这个类的一些属性方法,只能直接类名.属性名或类名.方法名。
      • @classmethod因为持有cls参数,可以来调用类的属性,类的方法,实例化对象等,避免硬编码。

Example 1:

>>> class a():

@staticmethod
def staticm():
   print 'static'

def normalm(self):
  print 'nomarl',self

@classmethod
def classm(cls):
   print 'class',cls

>>> a1=a()
>>> a1.normalm()
nomarl <__main__.a instance at 0x84dddec>
>>> a1.staticm()
static
>>> a1.classm()
class __main__.a
>>> type(a)
<type 'classobj'>
>>> type(a1)
<type 'instance'>

class ClassTest(object):
def __init__(self,a):
self.a = a
def printk(self):
print("a is ",self.a) @classmethod
def class_method(*args):
print("args is",args) @classmethod
def class_method2(cls):
print("cls is",cls ) @staticmethod
def static_method(*args):
print("args is",args) ct = ClassTest(100)
ct.printk() # a is 100
ct.class_method() # args is (<class '__main__.ClassTest'>,)
ct.class_method() # args is (<class '__main__.ClassTest'>,)
ct.static_method() # args is ()
ClassTest.class_method2() # 可以通过类直接调用 cls is <class '__main__.ClassTest'>
ClassTest.class_method() # args is (<class '__main__.ClassTest'>,)

 

模块复习 staticmethod和classmethod的区别的更多相关文章

  1. 基于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 ...

  2. Python中的@staticmethod和@classmethod的区别

    一直搞不明白,类方法和静态方法的区别,特意研究了一下,跟大家分享一下. 为了方便大家了解两者的差别,以下的示例代码将有助于发现其中的差别: class A(object): def foo(self, ...

  3. python(三)@staticmethod和@classmethod使用和区别

    转载自[1] 一般要用某个类的方法,先要实例这个类. 但是可以通过@staticmethod和@classmethod,直接用“类.方法()”来调用这个方法. 而 @staticmethod和@cla ...

  4. (转)关于python3中staticmethod(静态方法)classmethod(类方法)实例方法的联系和区别

    原文:http://dmcoders.com/2017/08/30/pythonclass/ https://zhuanlan.zhihu.com/p/28010894------正确理解Python ...

  5. python中 staticmethod与classmethod区别

    staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...

  6. @staticmethod和@classmethod的作用与区别

    一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...

  7. Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)

    原文地址:http://blog.csdn.net/caroline_wendy/article/details/23383995 还有一篇:http://blog.csdn.net/carolzha ...

  8. 【Python】@staticmethod和@classmethod的作用与区别

    前言 Python其实有3个方法,即静态方法(staticmethod),类方法(classmethod)和实例方法,一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法.而使用@static ...

  9. @staticmethod 和@classmethod区别

    python中@classmethod @staticmethod区别 Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. cla ...

随机推荐

  1. ubuntu安装ntp时间服务器

    1.安装ntp软件 sudo apt-get install ntp2.修改配置文件      sudo vim /etc/ntp.conf driftfile /var/lib/ntp/ntp.dr ...

  2. 基于angularJS的表单验证练习

    今天看了一下angularJS的表单验证,看的有点云里雾里(也有可能是雾霾吸多了),于是做了一个小练习来巩固一下. html: <div ng-controller="Aaa" ...

  3. 数据库~Mysql里的Explain说明

    对于mysql的执行计划可以在select前添加Explain来实现,它可以告诉我们你的语句性能如何. 下面是对explain的具体说明,也都是官方的,以后进行参考. id SELECT识别符.这是S ...

  4. Linux Directory Structure

    Note: Files are grouped according to purpose. Ex: commands, data files, documentation. Parts of a Un ...

  5. HighChart 体验之旅 (后台传递JSON参数和数据的方法)

    转自:http://www.cnblogs.com/daviddai/archive/2013/04/12/Highchart.html 官网:http://www.highcharts.com/ 中 ...

  6. HDFS Java Client对hdfs文件增删查改

      step1:增加依赖 pom.xml           ...      <!-- https://mvnrepository.com/artifact/org.apache.hadoop ...

  7. 第4章 scrapy爬取知名技术文章网站(2)

    4-8~9 编写spider爬取jobbole的所有文章 # -*- coding: utf-8 -*- import re import scrapy import datetime from sc ...

  8. 删除Panl控件中窗体的方法

    //删除窗体方法 private void CloseFrm() { foreach (Control item in panel1.Controls) { if (item is Form) //判 ...

  9. C# 程序执行时间差

    有时需要知道执行一个方法需要多少时间,这时会用到一个时间差TimeSpan DateTime startTime = DateTime.Now;//方法开始时间 //{ // 你需要测试的代码. // ...

  10. "类工厂模式"改写SqlHelper

    看到标题您一定很疑惑,23种经典设计模式什么时候多了一个"类工厂模式",稍等,请听我慢慢道来. 实践是检验真理的唯一途径.最近用了"类工厂模式"改写了我公司的S ...