描述
classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。 语法
classmethod 语法: classmethod
参数
无。
返回值
返回函数的类方法。 实例
以下实例展示了 classmethod 的使用方法: #!/usr/bin/python
# -*- coding: UTF-8 -*- class A(object):
bar = 1
def func1(self):
print ('foo')
@classmethod
def func2(cls):
print ('func2')
print (cls.bar)
cls().func1() # 调用 foo 方法 A.func2() # 不需要实例化

classmethod的更多相关文章

  1. python3 @classmethod 的使用场合

    官方的说法: classmethod(function)中文说明:classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下: class C: @clas ...

  2. python staticmethod and classmethod方法

    静态方法无绑定,和普通函数使用方法一样,只是需要通过类或者实例来调用.没有隐性参数. 实例方法针对的是实例,类方法针对的是类,他们都可以继承和重新定义,而静态方法则不能继承,可以认为是全局函数. #h ...

  3. 面对对象之@classmethod、@staticmethod用法

    @classmethod用法(修饰的函数,第一个参数cls默认是类名,调用方法:实例对象或类对象.方法) class C_mthod(object): name = "f**k" ...

  4. Python语言特性之3:@staticmethod和@classmethod

    问题:Python中@staticmethod和@classmethod两种装饰器装饰的函数有什么不同? 原地址:http://stackoverflow.com/questions/136097/w ...

  5. 【python】classmethod & staticmethod 区别

    来源:http://blog.csdn.net/carolzhang8406/article/details/6856817 其他参考: http://blog.csdn.net/lovingprin ...

  6. classmethod一个用处是创建可选类构造器

    Definition and Introduction通常来说, descriptor 是一种绑定着特殊行为属性的对象, 在访问它时行为被descriptor协议定义的方法所重载.这些方法是__get ...

  7. python 类中staticmethod,classmethod,普通方法

    1.staticmethod:静态方法和全局函数类似,但是通过类和对象调用. 2.classmethod:类方法和类相关的方法,第一个参数是class对象(不是实例对象).在python中class也 ...

  8. python为什么会有@classmethod?

    今天被问了这么个问题 python为什么要有classmethod? 被问倒了,只能回答:classmethod不需要实例化类,用起来比较方便.这么回答没有什么底细,于是查看了一下python的官方文 ...

  9. python 中类方法@classmethod

    classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下: class C: @classmethod def f(cls, arg1, arg2, .. ...

  10. 一些代码 I (斐波那契、for...else...、try和return、classmethod、统计个数)

    1. 斐波那契 from itertools import islice def fib(): a, b = 0, 1 while True: yield a a, b = b, a+b print ...

随机推荐

  1. Editing a Book UVA - 11212 IDA*

    You have n equal-length paragraphs numbered 1 to n . Now you want to arrange them in the order of 1 ...

  2. 集合Map映射(使用xml文件)

    Hibernate允许我们将Map元素与RDBMS进行映射. 我们知道,List和Map是基于索引的集合. 在map的情况下,索引列作为键,元素列用作值. 使用xml文件在集合映射中映射Map的示例 ...

  3. Java(System类,currentTimeMillis())

    CurrentTimeMillis()方法来记录程序的执行时间.currentTimeMillis()方法将返回自1970年1月1日午夜起到现在的时间,时间单位是ms,如果要记录程序中一段程序的运行时 ...

  4. ZJU 17th 校赛

    第一次参加校赛,和小伙伴们拿了7个气球,还是挺开心的.  简单记个流水账吧. A:判断出INF的情况后 暴力模拟即可. INF的情况有x=1 || y=1 || (x==2 && y= ...

  5. 从git上拉下来的严选weex项目demo

    项目地址 https://github.com/zwwill/yanxuan-weex-demo 在package.json里"author"之类后面加上 "privat ...

  6. Hadoop科普文—常见的45个问题解答 · Hadoop

    个模式 · 单机(本地)模式 · 伪分布式模式 · 全分布式模式 2.  单机(本地)模式中的注意点? 在单机模式(standalone)中不会存在守护进程,全部东西都执行在一个JVM上. 这里相同没 ...

  7. Android-ViewPagerIndicator框架使用——IconPageIndicator

    前言:IconPageIndicator是将自定义的图片作为指示图标的,这里的图片使用xml实现的. 1.自己定义图片: <?xml version="1.0" encodi ...

  8. MySQL中的聚合函数

    创建student表 CREATE TABLE IF NOT EXISTS `student` ( `id` int(4) unsigned NOT NULL AUTO_INCREMENT, `nam ...

  9. dva解读1

    1.首先定义一个app对象实现dva const app = dva({ history: createHistory(), }); // 2. Plugins app.use(createLoadi ...

  10. react 近期

    ECMAScript 6 入门:http://es6.ruanyifeng.com/#docs/destructuring#%E6%95%B0%E7%BB%84%E7%9A%84%E8%A7%A3%E ...