类方法@classmethod
通常情况下,如果我们要使用一个类的方法,那我们只能将一个类实体化成一个对象,进而调用对象使用方法。
式例 1
比如:
def __init__:
...
def print_hello(self):
print "Hello"
要用 print_hello() 就得:
hlo.print_hello()
Hello
如果用了 @classmethod 就简单了。
def __init__:
...
@classmethod
def print_hello(cls):
print "Hello"
要用的话直接:
Hello
注意:@classmethod 仅仅适用于单独的,与类本身的数据结构无关函数,其实用了它的函数,与使用普通函数无异,甚至不能在参数里加入 self,如果要在其中使用类的数据结构,仍然需要将类实例化一次才可以,所以要小心使用。
式例 2
class Room:
t = 1
def __init__(self):
pass @classmethod #classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数(python默认自动会加这样一个参数),可以来调用类的属性,类的方法,实例化对象等。
def status(cls,x): #cls 是由Room自动会传递过来,这是python做的
print(cls, x, cls.t) #<class '__main__.Room'> 10 1 Room.status(10) #Room 会自动传递给cls 10会传递给x
类方法@classmethod的更多相关文章
- python-静态方法staticmethod、类方法classmethod、属性方法property
Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def ...
- 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别
[面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...
- Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法
Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- python类方法@classmethod与@staticmethod
目录 python类方法@classmethod与@staticmethod 一.@classmethod 介绍 语法 举例 二.@staticmethod 介绍 语法 举例 python类方法@cl ...
- 静态方法staticmethod和类方法classmethod
静态方法staticmethod和类方法classmethod 一.类方法classmethod 把一个方法变成一个类中的方法,这个方法可以直接利用类来调用,不需要依托任何的对象,即不需要实例化也可以 ...
- Python - 静态方法@staticmethod和类方法classmethod
传送门 https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/Day09/%E9%9D%A2%E5%90%91%E5%AF ...
- python静态属性@property、类方法@classmethod、静态方法@staticmethod和普通方法
静态属性:即将类的函数通过@property属性封装,封装后实例调用该函数时,不再需要在函数后面加(),而是用类似调用数据属性的方式直接调用函数名称即可执行函数. 静态属性既可以访问类的属性,也可以访 ...
- 属性,类方法@classmethod
# 属性的初识# class Person:## def __init__(self,name,hight,weight):# self.name = name# self.__hight = hig ...
- Python面向对象 | 类方法 classmethod
类方法:必须通过类的调用,而且此方法的意义:就是对类里面的变量或者方法进行修改添加. 例一个商店,店庆全场八折,代码怎么写呢? class Goods: __discount = 0.8 # 折扣 ...
随机推荐
- Tomcat中Url中文乱码解决办法
引自:http://thoughtfly.iteye.com/blog/1533481 默认的tomcat容器如果直接使用get方式在url中传中文时,传到后台接收会是乱码. 乱码问题 原因: tom ...
- C#、AE开发入门之打开shp文件并显示
首先要建立好对应的对话框程序,并拖入axMapControl控件和axLisence控件,并设置一个按钮打开该文件 当然在使用前一定要绑定对应的ArcGis产品 static void Main() ...
- [转][Java]尝试解决Java多行字符串的编辑问题
转自:https://blog.csdn.net/jiuwuerliu/article/details/51207045 参考了:https://www.v2ex.com/amp/t/445522 除 ...
- 云端搭建内网局域网+NAT冗余上网:vps-centos6.10 +pptp client +2个ros 实现默认走pptp上网,万一pptp断了,走另外一个ros路由+centos7补充了下
介绍下环境: 1.ROS1也是PPTP SERVER,IP为172.16.22.3/24,pptp pool为172.16.23.0/24,pptp的默认帐号是111,密码是123 2.ROS2的IP ...
- [UE4]游戏主循环
游戏的运行模型 理解游戏的运行模型,对处理很多游戏错误有非常大的帮助. 游戏是有一个主循环的.那么游戏主循环做了什么事情呢? 游戏主循环一次就表示一帧,游戏主循环包括:接受输入.处理游戏逻辑.渲染.S ...
- Android给TextView设置多个字体颜色
效果如下:
- MCC MNC in china
A mobile country code (MCC) is used in combination with a mobile network code (MNC) (a combination k ...
- (转)C# BackgroundWorker组件的原理分析
原文地址:http://www.cnblogs.com/chaosimple/archive/2012/11/30/2796069.html 主要属性: 1.CancellationPending 获 ...
- ZooKeeper的安装和API
Zookeeper的分布式安装和API介绍: 安装教程 在datanode1.datanode2和datanode3三个节点上部署Zookeeper. 步骤 解压zookeeper安装包到/opt/m ...
- JQuery加载列表实现动画滚动(自上而下挤)
这个例子是jquery动态加载列表,并通过定时刷新,实现其循环滚动效果的一个例子. 1.HTML代码: <div class="fake-table"> <li ...