>>> class A(object):
bar = 1
def func1(self):
print 'foo' >>> class A(object):
bar = 1
def func1(self):
print 'foo'
@classmethod
def func2(cls):
print 'func2' >>> A.func2()#不需要实例化
func2
>>> A.func2
<bound method type.func2 of <class '__main__.A'>>>>> A.func1() Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
A.func1()
TypeError: unbound method func1() must be called with A instance as first argument (got nothing instead)
>>> A.bar
1

classmethod作用的更多相关文章

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

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

  2. python之内置装饰器(property/staticmethod/classmethod)

    python内置了property.staticmethod.classmethod三个装饰器,有时候我们也会用到,这里简单说明下 1.property 作用:顾名思义把函数装饰成属性 一般我们调用类 ...

  3. 关于Python中的classmethod

    Python 中的 classmethod classmethod: 作用是直接将自己的类对象,传给类方法. 一.classmethod 1)不用classmethod的时候 你的代码可能是这样写的, ...

  4. day10_friest_自动化

    一.知识回顾, 1.构造函数:def __del__(self)是类执行完后,需要将某些如连接等关闭,可将关闭代码写在该函数中,既是实例被销毁的时候执行 2.私有寒素:def __say(self)表 ...

  5. Python之路,第十八篇:Python入门与基础18

    python3  面向对象编程2 类方法: @classmethod 作用:1,类方法是只能访问类变量的方法: 2,类方法需要使用@classmethod 装饰器定义: 3,类方法的第一个参数是类的实 ...

  6. Python 【面向对象】

    前言 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触过面向对象的编程语言 ...

  7. python基础知识讲解——@classmethod和@staticmethod的作用

    python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...

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

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

  9. python @staticmethod和@classmethod的作用

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

随机推荐

  1. C/JS_实现选择排序

    1.js var arr = prompt("请输入一个数组(以“,”隔开):").split(",").map(function(data){ return ...

  2. log4j2自定义Appender(输出到文件/RPC服务中)

    1.背景 虽然log4j很强大,可以将日志输出到文件.DB.ES等.但是有时候确难免完全适合自己,此时我们就需要自定义Appender,使日志输出到指定的位置上. 本文,将通过两个例子说明自定义APP ...

  3. 开发入门,学Java还是学大数据?

    经常有人问,我想学习开发,到底是学Java好还是学大数据好?或者是,学习大数据还有必要学Java吗?      依我说,这个提问的标准答案是:两者都学.      先来甩两张图.      一张是腾讯 ...

  4. c++中函数的参数传递,内联函数和默认实参的理解

    1.参数传递 1)函数调用时,c++中有三种传递方法:值传递.指针传递.引用传递. 给函数传递参数,遵循变量初始化规则.非引用类型的形参一相应的实参的副本初始化.对(非引用)形参的任何修改仅作用域局部 ...

  5. php 通过 create user 和grant 命令无法创建数据库用户和授权的解决办法

    php 通过 create user 和grant 命令无法创建数据库用户和授权的解决办法 解决办法, 通过 insert 命令的方式进行创建. 创建数据库用户: $sql= "insert ...

  6. js实现图片旋转

    1.以下代码适用ie9版本 js代码如下: function rotate(o,p){ var img = document.getElementById(o); if(!img || !p) ret ...

  7. Data type conversion in MongoDB

    [问题] I have a collection called Document in MongoDB. Documents in this collection have a field calle ...

  8. 高斯模糊的Java实现

    1.http://jhlabs.com/ip/index.html public static byte[] blur(byte[] data) throws IOException { ByteAr ...

  9. Android: Avoid passing null as the view root

    在做一个应用时把Android SDK从4.4换成6.0,使用LayoutInflater的inflate方法时出现以下情 LayoutInflater.inflate(int resource, V ...

  10. 【ASP.NET Core】浅说目录浏览

    何谓“浅说”?就是一句话说不完,顶多两句话就介绍完毕,然后直接给上实例的解说方式.化繁为简,从七千年前到现在,从老祖宗到咱们,一直都在追求的理想目标,尽可能把复杂的东西变成简单的. 老周告诉你一个可以 ...