class my_class():
args1=1
def fun(self):
print self.args1
my_c=my_class()
my_c.fun()
请问在类里,那个self.args1 是类变量还是实例变量?类变量可以用self.args1用吗?
怪我咯2017-04-17 17:42:17
If you understand the class space and instance space, this problem will be very simple. The class has its own namespace, which can be passed className.__dict__
查看得到里面的内容。 同样实例化一个实例的话, 那么这个实例也就有了自己的名字空间,用来存储实例的成员,方法等。当然一个实例肯定可以共享类的成员的。
具体对你的case来说,如果print self.args1
, 首先会在实例的名字空间内部查找, 没有发现args1
这个成员,于是继续向上找到类的空间,发现有了args1,这样就会输出类的args1. 但是如果有赋值语句的话,比如self.args1 = 1
, then the instance will create the args1 member in its own space. Look at the code below:
In [14]: class A(object):
....: args1 = 1
....: def fun(self):
....: print self.args1
....: def fun2(self):
....: self.args1 = 2
In [15]: a = A()
In [16]: A.__dict__ # 这是类的名字空间,可以看到args1在里面
Out[16]: #删除了一些
{ 'args1': 1,
'fun': <function __main__.fun>,
'fun2': <function __main__.fun2>}
In [17]: a.__dict__ #而实例a的名字空间则没有args1
Out[17]: {}
In [18]: a.fun() #执行func 则是查找到了类的args1的成员。
1
In [19]: a.fun2() #执行了func2, 这就会在a这个实例空间里面创建args1这个成员,与类的无关。
In [20]: a.__dict__ #可以看到a的空间里面已经 有了args1.
Out[20]: {'args1': 2}
In [21]: a.fun() #再次执行,则会直接输出实例a自己的args1.
2
In [22]: A.args1 #类与实例的空间互不影响。
Out[22]: 1
In [23]: b = A() #实例之间的空间也是相互独立,
In [24]: b.args1
Out[24]: 1
Combined with the above code, you should be able to understand it from a relative essence
PHPz2017-04-17 17:42:17
Class variable, this can be verified by directly printing the id of the class object and whether the object id restricted by self is the same. The address of the class and the object id are the same. The reason why self can be used is because python first searches for instance variables by default. The class variable cannot be found, so it appears to be an object variable, but it actually refers to a class variable.
class MyClass:
args1 = 1
def fun(self):
print id(self.args1)
my_c = MyClass()
print id(MyClass.args1)
my_c.fun()
If you look deeper, in fact, self here only represents the bound object. You can also name it with other variable names
class MyClass:
args1 = 1
def fun(binding): #这里用的是binding,而不是self
print id(binding.args1)
my_c = MyClass()
print id(MyClass.args1)
my_c.fun()
ringa_lee2017-04-17 17:42:17
class myclass():
args = 1
def fun(self):
print self.args
self.__class__.args += 1
if __name__ == '__main__':
m1 = myclass()
m1.fun()
m2 = myclass()
m2.fun()
The result is
1
2
Obviously a class variable
PHP中文网2017-04-17 17:42:17
self.args1
这是实例变量。my_class.args1
This is a class variable.
class my_class():
args1=1
def fun(self):
print self.args1 #这是实例变量
print my_class.args1 #这是类变量
ringa_lee2017-04-17 17:42:17
self.args1 This is an instance variable.
my_class.args1 This is the class variable.