使用静态变量了解 self 的行为
Understanding behaviour of self with static variables
在 python.From 中处理静态变量时,我对 self 的行为感到困惑,我的理解是可以使用 classname.variablename
或 [=13= 访问静态变量].但是,更改该变量的值是不同的。我意识到,如果我通过 classname.variablename=SomeValue
更改静态变量值,实例变量会反映该值,但是如果我使用 self.variablename=SomeValue
更改静态变量的值,则静态变量在访问时不会更改 classname.variablename
据我了解,当我分配一个像 self.variablename=SomeValue
这样的值时,就会创建一个实例变量。有人可以解释一下这种行为吗?
示例 1:
class bean:
mycar="SomeCar"
def test(self):
bean.mycar = "yup"
print(self.mycar) #prints yup
示例 2:
class bean:
mycar="SomeCar"
def test(self):
self.mycar = "pup"
print(bean.mycar) #SomeCar
python 中的 self
是一个普通名称,它将引用绑定到调用 class 方法的 实例 。它作为第一个参数传递给方法,按照惯例,它绑定到名称 'self'。当调用 self.variable = value
时,您正在设置实例变量的值; 特别 bean
的唯一变量。
例如,self.name = "Fred"
可能会命名我妈妈的豆子,但我在从我的调用self.name
时将我自己的豆子命名为"George"豆。
另一方面,bean.name = "Yousef"
命名所有个bean。我妈妈的豆子现在被命名为 "Yousef",我的也是。
如果我爸爸也有一颗豆子,当他打电话给 bean.name
时,他会惊讶地发现它也被命名为 "Yousef"。但他仍然可以使用 self.name
为他的 bean 命名(可能是唯一的)。
示例代码:
class bean:
name = "Yousef" # All beans have this name with `bean.name`
moms = bean()
mine = bean()
dads = bean()
beans = [moms, mine, dads]
# Primitive tabular output function
def output(bean_list):
print("-bean-", "\t", "-self-")
for b in bean_list:
print(bean.name, "\t", b.name)
print("") # Separate output sets with a newline
# Print the names with only the class attribute set
output(beans)
# Python magic using zip to apply names simultaneously
# Mom's bean is "Fred", mine is "George"
# My dad is weird and named his "Ziggaloo"
for b, n in zip(beans, ["Fred", "George", "Ziggaloo"]):
b.name = n
# Print the names after applying `self.name`
output(beans)
Python 3.4 输出:
-bean- -self-
Yousef Yousef
Yousef Yousef
Yousef Yousef
-bean- -self-
Yousef Fred
Yousef George
Yousef Ziggaloo
class实体和实例都可以有属性。
class 属性已分配给 class 对象。人们有时称其为 "static variable".
一个实例属性被分配给一个实例("instance variable")。
当对象的属性为 read 时,会发生许多事情(参见 Descriptor HowTo Guide),但简短版本为:
- 尝试从实例中读取属性
- 如果失败,请尝试从 class
中读取
写成的时候,那就没有这个机制了。它写在它写的地方;)
见示例:
class A(object):
pass
a = A()
print A.value # fails - there is no "value" attribute
print a.value # fails - there is no "value" attribute
A.value = 7
print A.value # prints 7
print a.value # also prints 7 - there is no attribute on instance, but there is on class
a.value = 11
print A.value # prints 7
print a.value # prints 11 - now there is an attribute on the instance
a2 = A()
print a2.value # prints 7 - this instance has no "value", but the class has
自己?
顺便说一句,self
参数(在问题中)是一个实例,就像 a
在这里一样。
在 python.From 中处理静态变量时,我对 self 的行为感到困惑,我的理解是可以使用 classname.variablename
或 [=13= 访问静态变量].但是,更改该变量的值是不同的。我意识到,如果我通过 classname.variablename=SomeValue
更改静态变量值,实例变量会反映该值,但是如果我使用 self.variablename=SomeValue
更改静态变量的值,则静态变量在访问时不会更改 classname.variablename
据我了解,当我分配一个像 self.variablename=SomeValue
这样的值时,就会创建一个实例变量。有人可以解释一下这种行为吗?
示例 1:
class bean:
mycar="SomeCar"
def test(self):
bean.mycar = "yup"
print(self.mycar) #prints yup
示例 2:
class bean:
mycar="SomeCar"
def test(self):
self.mycar = "pup"
print(bean.mycar) #SomeCar
self
是一个普通名称,它将引用绑定到调用 class 方法的 实例 。它作为第一个参数传递给方法,按照惯例,它绑定到名称 'self'。当调用 self.variable = value
时,您正在设置实例变量的值; 特别 bean
的唯一变量。
例如,self.name = "Fred"
可能会命名我妈妈的豆子,但我在从我的调用self.name
时将我自己的豆子命名为"George"豆。
另一方面,bean.name = "Yousef"
命名所有个bean。我妈妈的豆子现在被命名为 "Yousef",我的也是。
如果我爸爸也有一颗豆子,当他打电话给 bean.name
时,他会惊讶地发现它也被命名为 "Yousef"。但他仍然可以使用 self.name
为他的 bean 命名(可能是唯一的)。
示例代码:
class bean:
name = "Yousef" # All beans have this name with `bean.name`
moms = bean()
mine = bean()
dads = bean()
beans = [moms, mine, dads]
# Primitive tabular output function
def output(bean_list):
print("-bean-", "\t", "-self-")
for b in bean_list:
print(bean.name, "\t", b.name)
print("") # Separate output sets with a newline
# Print the names with only the class attribute set
output(beans)
# Python magic using zip to apply names simultaneously
# Mom's bean is "Fred", mine is "George"
# My dad is weird and named his "Ziggaloo"
for b, n in zip(beans, ["Fred", "George", "Ziggaloo"]):
b.name = n
# Print the names after applying `self.name`
output(beans)
Python 3.4 输出:
-bean- -self-
Yousef Yousef
Yousef Yousef
Yousef Yousef
-bean- -self-
Yousef Fred
Yousef George
Yousef Ziggaloo
class实体和实例都可以有属性。
class 属性已分配给 class 对象。人们有时称其为 "static variable".
一个实例属性被分配给一个实例("instance variable")。
当对象的属性为 read 时,会发生许多事情(参见 Descriptor HowTo Guide),但简短版本为:
- 尝试从实例中读取属性
- 如果失败,请尝试从 class 中读取
写成的时候,那就没有这个机制了。它写在它写的地方;)
见示例:
class A(object):
pass
a = A()
print A.value # fails - there is no "value" attribute
print a.value # fails - there is no "value" attribute
A.value = 7
print A.value # prints 7
print a.value # also prints 7 - there is no attribute on instance, but there is on class
a.value = 11
print A.value # prints 7
print a.value # prints 11 - now there is an attribute on the instance
a2 = A()
print a2.value # prints 7 - this instance has no "value", but the class has
自己?
顺便说一句,self
参数(在问题中)是一个实例,就像 a
在这里一样。