Python中self的含义:
1.这里的自己,指的是,实例Instance本身,当前类的实例。
2.同时, 由于说到“自己”这个词,都是和相对而言的“其他”而说的;而此处的其他,指的是,类Class,和其他变量,比如局部变量,全局变量等。
有时候,虽然你写的代码,可以运行,但是使用到的变量,由于没有加self,实际上是用到的不是实例的变量,而是其他的变量。
此类问题,主要和Python中的变量的作用域有关,但是此处例子中,也和是否使用self有关:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: antcolonies
name = 'whole global name'
'''
注:此处全局的变量名,写成name,只是为了演示而用
实际上,好的编程风格,应该写成gName之类的名字,
以表示该变量是Global的变量
'''
class Person(object):
name = 'class global name'
def __init__(self, newPersonName):
# self.name = newPersonName
'''
此处,没有使用self.name
而使得此处的name,实际上仍是局部变量name
虽然此处赋值了,但是后面没有被利用到,属于被浪费了的局部变量name
'''
name = newPersonName
def sayYourName(self):
'''
此处,之所以没有像之前一样出现:
AttributeError: Person instance has no attribute 'name'
那是因为,虽然当前的实例self中,没有在__init__中初始化对应的name变量,实例self中没有对应的name变量
但是由于实例所对应的类Person,有对应的name变量,所以也是可以正常执行代码的
对应的,此处的self.name,实际上是Person.name
'''
print('My name is %s' %self.name)
print('Name within class Person is actually the global name: %s' %name)
print("Only access Person's name via Person.name = %s" %(Person.name))
def selfAndInitDemo():
personInstance = Person('Tim')
personInstance.sayYourName()
print('whole global name is %s' %name)
if __name__ == '__main__':
selfAndInitDemo()
'''
My name is class global name
Name within class Person is actually the global name: whole global name
Only access Person's name via Person.name = class global name
whole global name is whole global name
'''