python的cls,self,classmethod,staticmethod
python类里会出现这三个单词,self和cls都可以用别的单词代替,类的方法有三种,
一是通过def定义的 普通的一般的,需要至少传递一个参数,一般用self,这样的方法必须通过一个类的实例去访问,类似于c++中通过对象去访问;
二是在def前面加上@classmethod,这种类方法的一个特点就是可以通过类名去调用,但是也必须传递一个参数,一般用cls表示class,表示可以通过类直接调用;
三是在def前面加上@staticmethod,这种类方法是静态的类方法,类似于c++的静态函数,他的一个特点是参数可以为空,同样支持类名和对象两种调用方式;
代码:
- class A:
- member = "this is a test."
- def __init__(self):
- pass
- @classmethod
- def Print1(cls):
- print "print 1: ", cls.member
- def Print2(self):
- print "print 2: ", self.member
- @classmethod
- def Print3(paraTest):
- print "print 3: ", paraTest.member
- @staticmethod
- def print4():
- print "hello"
- a = A()
- A.Print1()
- a.Print1()
- #A.Print2()
- a.Print2()
- A.Print3()
- a.Print3()
- A.print4()
python的cls,self,classmethod,staticmethod的更多相关文章
- python基础知识讲解——@classmethod和@staticmethod的作用
python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...
- python -- @classmethod @staticmethod区别和使用
python中的定义: class MyClass: ... @classmethod # classmethod的修饰符 def class_method(cls, arg1, arg2, ... ...
- 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别
[面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...
- python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?
回答背景知识 这些都是装饰器(decorator).装饰器是一种特殊的函数,要么接受函数作为输入参数,并返回一个函数,要么接受一个类作为输入参数,并返回一个类. @标记是语法糖(syntactic s ...
- Python类中装饰器classmethod,staticmethod,property,
@classmethod 有的时候在类中会有一种情况,就是这个方法并不需要使用每一个对象属性 因此 这个方法中的self参数一个完全无用的参数,使用classmethod class A: __cou ...
- 初识面向对象(钻石继承,super,多态,封装,method,property,classmethod,staticmethod)
组合 什么有什么的关系 一个类的对象作为另一个类的对象继承 子类可以使用父类中的名字(静态属性 方法)抽象类和接口类 只能不继承,不能被实例化 子类必须实现父类中的同名方法———规范代码 metacl ...
- 第7.18节 案例详解:Python类中装饰器@staticmethod定义的静态方法
第7.18节 案例详解:Python类中装饰器@staticmethod定义的静态方法 上节介绍了Python中类的静态方法,本节将结合案例详细说明相关内容. 一. 案例说明 本节定义了类Sta ...
- python为什么会有@classmethod?
今天被问了这么个问题 python为什么要有classmethod? 被问倒了,只能回答:classmethod不需要实例化类,用起来比较方便.这么回答没有什么底细,于是查看了一下python的官方文 ...
- @classmethod @staticmethod 个人理解
官方解释 @classmethod 一个类方法把类自己作为第一个实参, 就像一个实例方法把实例自己作为第一个实参. 语法格式: class C: @classmethod def f(cls, arg ...
- Python面向对象中的classmethod类方法和__getattr__方法介绍
一.classmethod介绍 介绍:@classmethod修饰符我们从名称就可以知道,这是一个类方法,那么和普通的类中的方法有什么不同的 a.类方法,是由类本身调用的,无需实例化类,直接用类本身调 ...
随机推荐
- man da'te
DATE(1) 用户命令 DATE(1) 名称 日期-打印或设置系统日期和时间 简介 date [OPTION]... ...
- Android 如何生成jks签名文件
- web移动端适配方案
web移动端常用解决方案: 一.通过js+rem,这里有一个解决方案(http://imochen.github.io/hotcss/) 1.1.rem兼容性(https://caniuse.com) ...
- CF1012F Passports
http://codeforces.com/problemset/problem/1012/F 题解 考虑\(p=1\)的情况. 我们可以把题意理解成平面上有一些线段,你需要给每条线段分配一个长度给定 ...
- 解决Acunetix 12中文汉化的方法
最近下载一款测试软件acunetix,苦于满屏英文的苦恼,看不懂,于是乎就问度娘,结果度娘就是给中文破解包: 我是12版的,网上提供的都是11版的,没法用.怎么办呢?还好我是做测试的,平时做兼容性测试 ...
- Broken pipe
出现broken pipe 的一种情况是向socket写数据,但是对端已经关闭socket连接,此时会触发SIGPIPE信号,该信号可以捕获. signal(SIGPIPE, SIG_IGN);
- 使用eclipse导入新项目时中文出现乱码问题
有时候在github上看到别人不错的项目想要拉下来学习学习的时候,总会出现这样的情况,实在蛋疼. 一般出现这种问题,会有三个地方需要改动: 在项目上右键选择 properties 将 text fil ...
- JavaScript modularity with RequireJS (from spaghetti code to ravioli code)
http://netmvc.blogspot.com/2012/11/javascript-modularity-with-requirejs.html Today I would like to d ...
- Git remotes/origin/pr/* 分支清理,代码回退等
代码在gitHub上托管,每次git pull完后,用git branch -a都可以看到一堆remotes/origin/pr/*分支: 可以通过两种方式去除: 1,修改git的config文件找到 ...
- cookie格式化
#coding=utf- import requests url = 'http://www.baidu.com' f=open(r'cookies.txt','r') cookies={} for ...