Python学习笔记1:Self
2016-11-03 14:57
260 查看
转载网址:http://python.jobbole.com/81921/ 代码: class A(): def __init__(self,a,b): self.a =a self.b =b def add(self): print(self.a+self.b) B =A(3,4) B.add()结果:
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
======================= RESTART: D:/selenium/test/5.py =======================
7
>>> __init__()方法为类A的初始化函数,一般在创建类时会首先声明这个函数,而add方法可以直接拿初始化方法的self.a和self.b两个参数进行计算
class Test: def prt(self): print(self) print(self.__class__) def prt1(this): print(this) print(this.__class__) def prt2(): print(__class__) def __get__(self, ins, cls): #print('self in Desc: %s ' % self ) print(self, ins, cls) t = Test() t.__get__('ad','cs')
总结
self在定义时需要定义,但在调用时则会自动传入,不需要再手动传入self的名字不是固定的,按约定使用self
self总是指调用时类的实例
相关文章推荐
- Python学习笔记(1)——Python为什么要self
- python_小白的修炼笔记_2018/1/4_self和__init__()学习
- Python学习笔记:Python Language Features
- Boost.Python学习笔记
- Python 学习笔记 - 4.if 表达式
- python文件读写实例学习笔记
- Python 学习笔记(1)
- python(异常处理机制,学习笔记摘要)
- python学习笔记(1)
- python学习笔记,整形,字符串
- Python学习笔记1
- Python学习笔记
- if __name__ (python学习笔记一,注意前面是双下划线)
- python学习笔记(二)
- python学习笔记(2)
- python学习笔记1
- Python学习笔记(一)
- [原创]Ruby学习笔记(4)-闰年、季节和月份天数的Ruby版和Python版
- Python学习笔记 Module
- Python 正则式学习笔记