Python中self的理解(一)
来源:互联网 发布:sql不包含某个字符串 编辑:程序博客网 时间:2024/06/05 06:40
首先明确的是self只有在类的方法中才会有,独立的函数或方法是不必带有self的。self在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数。
self名称不是必须的,在python中self不是关键词,你可以定义成a或b或其它名字都可以,但是约定成俗,不要搞另类,大家会不明白的。下例中将self改为myname一样没有错误:
class Person: def _init_(myname,name): myname.name=name def sayhello(myname): print 'My name is:',myname.namep=Person('Bill')print pself指的是类实例对象本身(注意:不是类本身)。
class Person: def _init_(self,name): self.name=name def sayhello(self): print 'My name is:',self.namep=Person('Bill')print p
在上述例子中,
self
指向Person的实例p。 为什么不是指向类本身呢,如下例子:
class Person: def _init_(self,name): self.name=name def sayhello(self): print 'My name is:',self.namep=Person('Bill')p1 = Person('Apple')print p如果self指向类本身,那么当有多个实例对象时,self指向哪一个呢?
0 0
- Python中self的理解(一)
- Python中self的理解
- Python中self的理解
- Python中self的理解
- Python中self的理解
- Python中默认参数self的理解
- Python中默认参数self的理解
- Python中self的理解(二)
- Python中默认参数self的理解
- 关于Python中定义类时 self 的理解
- Python中默认参数self的理解[转]
- Python中self的含义
- python中self的作用
- Python中self的含义
- Python中self的含义
- [python]对self和_init_的理解
- python学习过程对self的理解
- python中的self理解
- 静态顺序表的各种操作
- popupWindow弹出
- nexus本地私服对应的maven一些配置问题
- day14 Python css positon学习 之 fixed
- hdu 5900 QSC and Master
- Python中self的理解(一)
- Codeforces-327A-Flipping Game
- MapReduce概念以及运行机制
- freeMarker学习(三)
- 集合的子集
- Mybatis的分页插件PageHelper4.1.6的使用
- BZOJ 1367 [Baltic2004]sequence 解题报告
- poj 1363 Rails(栈)
- lua常用函数