【python】self & cls
- 普通的方法,第一个参数需要是self,它表示一个具体的实例本身。
- 如果用了staticmethod,那么就可以无视这个self,而将这个方法当成一个普通的函数使用。
- 对于classmethod,它的第一个参数不是self,是cls,它表示这个类本身。
- 类先调用__new__方法,返回该类的实例对象。这个实例对象就是__init__方法的第一个参数self,即self是__new__的返回值
举个栗子如下:
class A(object):
def foo1(self):
print "Hello",self
@staticmethod
def foo2():
print "hello"
@classmethod
def foo3(cls):
print "hello",cls
>>> a = A()
>>> a.foo1() #最常见的调用方式,但与下面的方式相同
Hello <__main__.A object at 0x9f6abec>
>>> A.foo1(a) #这里传入实例a,相当于普通方法的self
Hello <__main__.A object at 0x9f6abec>
>>> A.foo2() #这里,由于静态方法没有参数,故可以不传东西
hello
>>> A.foo3() #这里,由于是类方法,因此,它的第一个参数为类本身。
hello <class '__main__.A'>
>>> A #可以看到,直接输入A,与上面那种调用返回同样的信息。
<class '__main__.A'>
【python】self & cls的更多相关文章
- 【Python②】python之首秀
第一个python程序 再次说明:后面所有代码均为Python 3.3.2版本(运行环境:Windows7)编写. 安装配置好python后,我们先来写第一个python程序.打开IDLE (P ...
- 【python】多进程锁multiprocess.Lock
[python]多进程锁multiprocess.Lock 2013-09-13 13:48 11613人阅读 评论(2) 收藏 举报 分类: Python(38) 同步的方法基本与多线程相同. ...
- 【python】SQLAlchemy
来源:廖雪峰 对比:[python]在python中调用mysql 注意连接数据库方式和数据操作方式! 今天发现了个处理数据库的好东西:SQLAlchemy 一般python处理mysql之类的数据库 ...
- 【python】getopt使用
来源:http://blog.chinaunix.net/uid-21566578-id-438233.html 注意对比:[python]argparse模块 作者:limodou版权所有limod ...
- 【Python】如何安装easy_install?
[Python]如何安装easy_install? http://jingyan.baidu.com/article/b907e627e78fe146e7891c25.html easy_instal ...
- 【Python】 零碎知识积累 II
[Python] 零碎知识积累 II ■ 函数的参数默认值在函数定义时确定并保存在内存中,调用函数时不会在内存中新开辟一块空间然后用参数默认值重新赋值,而是单纯地引用这个参数原来的地址.这就带来了一个 ...
- 【Python】-NO.97.Note.2.Python -【Python 基本数据类型】
1.0.0 Summary Tittle:[Python]-NO.97.Note.2.Python -[Python 基本数据类型] Style:Python Series:Python Since: ...
- 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】
1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...
- 【Python】-NO.98.Note.3.Python -【Python3 解释器、运算符】
1.0.0 Summary Tittle:[Python]-NO.98.Note.3.Python -[Python3 解释器] Style:Python Series:Python Since:20 ...
随机推荐
- DOMContentLoaded方法
document.addEventListener('DOMContentLoaded',function(){ alert("SSDD") },false);
- 查看值是否传过来php
<input value='{{mid}}'></input> <input value='{{share}}'></input>
- 如何分析java内存泄漏问题
java中的内存泄漏首先需要dump文件出来,主要包括内存dump.线程dump: 内存dump是指通过jmap -dump <pid>输出的文件,而线程dump是指通过jstack &l ...
- hello2部分代码解析
/** * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.** You may not modify, us ...
- Vue开发中遇到的问题及解决方案
问题一:npm run dev的时候控制台报错Vue packages version mismatch,如下面 可是检查package.json文件里vue和vue-template-compile ...
- mysql查询出相同数据出现的次数,统计相同值的数量
1.可以使用count SELECT count(name='A' OR NULL) FROM table 2.用sum SELECT sum(if( = 'A', 1, 0)) FROM table ...
- gnu make - 初学
因为要为Linux平台编译ACE,按照ACE的文档如何编译部分的说明,要求使用gnu make.其原文档说明如下: Using the Traditional ACE/GNU Configuratio ...
- 兼容性 memo
一.async 与 defer <script src="js/require.js" defer async="true" ></scrip ...
- [Leetcode 90]求含有重复数的子集 Subset II
[题目] Given a collection of integers that might contain duplicates, nums, return all possible subsets ...
- LeetCode 695 岛屿的最大面积
题目: 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被水包围着. 找到给定的二 ...