类可变访问和分配 [英] Class Variable Access and Assignment

查看:52
本文介绍了类可变访问和分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与类变量和实例变量有关。


给出以下内容:


< code>


class _class:

var = 0

#rest of the class


instance_b = _class ()


_class.var = 5

print instance_b.var# - > 5

print _class.var# - > 5


< / code>


最初这似乎有道理,请注意最后的差异

两行,一行通过类

引用类变量''var'',而另一行通过实例引用它。


但是如果有人尝试以下方法:


< code>


instance_b.var = 1000# - > _class.var = 5

_class.var = 9999# - > _class.var = 9999



发生明显错误。当试图通过实例分配类变量

时,它会在该实例中创建一个新条目'

__dict__并为其赋值。虽然这是允许的,因为

pythons能够动态地向实例添加属性但是它对于不同的操作有不同的行为似乎是不正确的。

有两种可能的修复,要么通过禁止实例变量

与类变量同名,这将允许任何引用

到一个实例class赋值/读取变量的值。或者

只允许通过类名本身访问类变量。


非常感谢elpargo和可乐。 elpargo帮助充实了

最好的方式来呈现这个。


或许这是有意的,我只是想知道是否有其他人有

注意到它,如果是这样,你认为什么形式是''正确的''

要么通过类本身引用类变量,要么通过

实例那个班。任何回复都将不胜感激。

格雷厄姆

解决方案

2005年11月3日星期四01:43 :32 -0800,Graham写道:


[snip]

print instance_b.var# - > 5
print _class.var# - > 5

< / code>

最初这似乎有道理,注意最后两行之间的区别,一个是引用类变量' 'var''通过类
而另一个通过实例引用它。


这是不正确的。行instance_b.var指的是一个实例

属性。根据通常的面向对象的继承模型,如果

实例没有属性,则接下来搜索该类。


所以instance_b.var和_class.var要求两个不同的东西。

首先说,搜索实例的属性var,然后是类。

秒表示搜索课程。


BTW,领先的下划线是私有(ish)变量的约定。

在保留字后命名变量的约定是尾随

下划线class_。在这种情况下,还有一个约定

应该以大写字母开头,因此你有Class和instance。 (例如,

课程,不是保留字。)


但是,如果有人尝试以下方法:

<代码>

instance_b.var = 1000# - > _class.var = 5
_class.var = 9999# - > _class.var = 9999
< / code>

发生明显的错误。


我没有看到错误。我尝试时没有异常:我得到了预期的

结果。分配给实例分配给实例,分配给该类分配给该类的
。这是正常的OO行为。


当试图通过实例分配类变量时,它会在该实例中创建一个新条目'
__dict__并给它值。


您可能*希望*分配给class属性,但那不是你b
。你正在分配给实例。


不可否认,它可能不是你期望的行为,但它是

标准行为(就我而言)所有OO语言。


如果要分配给class属性,可以直接分配给

类,也可以使用instance_b .__ class__。 var。


虽然这是允许的,因为
pythons能够动态地向实例添加属性但是对于不同的操作来说它具有不同的行为似乎是不正确的。


当然你不是那个意思吗?为什么你想要不同的操作

有相同的行为?


有两种可能的修复,要么通过禁止实例变量
同样的将类命名为类变量,这将允许对类的实例的任何引用分配/读取变量的值。或者只允许通过类名本身访问类变量。




还有第三个修复:理解Python的OO模型,特别是

继承,所以正常行为不再让你感到惊讶。

-

史蒂文。


Op 2005-11-03,Steven D''Aprano schreef< st *** @ REMOVETHIScyber.com.au>:

有两种可能的修复方式,要么通过禁止实例变量
与类变量同名,这将允许对类的实例的任何引用分配/读取值变量。或者只允许通过类名本身访问类变量。



还有第三个修复:理解Python的OO模型,尤其是
继承,这样正常的行为不再让你感到惊讶。




无论是OO模型是什么,我都不认为以下代码

表现出理智的行为:


A类:

a = 1


b = A()

ba + = 2

打印ba

打印Aa


结果为


3

1

-

Antoon Pardon

< br>

Antoon Pardon< ap ***** @ forel.vub.ac.be>写道:

Op 2005-11-03,Steven D''Aprano schreef< st *** @ REMOVETHIScyber.com.au>:

有两种可能的修复方法,要么禁止实例变量
与类变量同名,这样就可以对类的实例进行任何引用/读取变量的值。或者只允许通过类名本身访问类变量。



还有第三个修复:理解Python的OO模型,尤其是
继承,这样正常的行为不再让你感到惊讶。



无论是OO模型,我都不认为以下代码表现出明智的行为: br />
A类:
a = 1

b = A()
ba + = 2
打印ba
打印Aa

结果

3
1




我一开始觉得很困惑但是我要明白发生了什么:-)


但是真的,这里应该做些什么呢?


S.


This has to do with class variables and instances variables.

Given the following:

<code>

class _class:
var = 0
#rest of the class

instance_b = _class()

_class.var=5

print instance_b.var # -> 5
print _class.var # -> 5

</code>

Initially this seems to make sense, note the difference between to last
two lines, one is refering to the class variable ''var'' via the class
while the other refers to it via an instance.

However if one attempts the following:

<code>

instance_b.var = 1000 # -> _class.var = 5
_class.var = 9999 # -> _class.var = 9999

</code>

An obvious error occurs. When attempting to assign the class variable
via the instance it instead creates a new entry in that instance''s
__dict__ and gives it the value. While this is allowed because of
pythons ability to dynamically add attributes to a instance however it
seems incorrect to have different behavior for different operations.

There are two possible fixes, either by prohibiting instance variables
with the same name as class variables, which would allow any reference
to an instance of the class assign/read the value of the variable. Or
to only allow class variables to be accessed via the class name itself.

Many thanks to elpargo and coke. elpargo assisted in fleshing out the
best way to present this.

perhaps this was intended, i was just wondering if anyone else had
noticed it, and if so what form would you consider to be ''proper''
either referring to class variables via the class itself or via
instances of that class. Any response would be greatly appreciated.
Graham

解决方案

On Thu, 03 Nov 2005 01:43:32 -0800, Graham wrote:

[snip]

print instance_b.var # -> 5
print _class.var # -> 5

</code>

Initially this seems to make sense, note the difference between to last
two lines, one is refering to the class variable ''var'' via the class
while the other refers to it via an instance.
That''s not correct. The line instance_b.var is referring to an instance
attribute. According to the usual Object Oriented model of inheritance, if
the instance does not have an attribute, the class is searched next.

So instance_b.var and _class.var are asking for two different things. The
first says, "Search the instance for attribute var, then the class." The
second says "Search the class."

BTW, a leading underscore is the convention for a private(ish) variable.
The convention for naming a variable after a reserved word is a trailing
underscore class_. In this case, there is also the convention that classes
should start with a capital, so you have Class and instance. (instance, of
course, is not a reserved word.)

However if one attempts the following:

<code>

instance_b.var = 1000 # -> _class.var = 5
_class.var = 9999 # -> _class.var = 9999

</code>

An obvious error occurs.
I see no error. No exception is raised when I try it: I get the expected
results. Assigning to an instance assigns to the instance, assigning to
the class assigns to the class. That''s normal OO behaviour.

When attempting to assign the class variable
via the instance it instead creates a new entry in that instance''s
__dict__ and gives it the value.
You might *want* to assign to the class attribute, but that''s not what you
are doing. You are assigning to the instance.

Admittedly, it might not be the behaviour you expect, but it is the
standard behaviour in (as far as I know) all OO languages.

If you want to assign to the class attribute, you either assign to the
class directly, or use instance_b.__class__.var.

While this is allowed because of
pythons ability to dynamically add attributes to a instance however it
seems incorrect to have different behavior for different operations.
Surely you can''t mean that? Why would you want different operations to
have the same behaviour?

There are two possible fixes, either by prohibiting instance variables
with the same name as class variables, which would allow any reference
to an instance of the class assign/read the value of the variable. Or
to only allow class variables to be accessed via the class name itself.



There is also a third fix: understand Python''s OO model, especially
inheritance, so that normal behaviour no longer surprises you.
--
Steven.


Op 2005-11-03, Steven D''Aprano schreef <st***@REMOVETHIScyber.com.au>:

There are two possible fixes, either by prohibiting instance variables
with the same name as class variables, which would allow any reference
to an instance of the class assign/read the value of the variable. Or
to only allow class variables to be accessed via the class name itself.



There is also a third fix: understand Python''s OO model, especially
inheritance, so that normal behaviour no longer surprises you.



No matter wat the OO model is, I don''t think the following code
exhibits sane behaviour:

class A:
a = 1

b = A()
b.a += 2
print b.a
print A.a

Which results in

3
1

--
Antoon Pardon


Antoon Pardon <ap*****@forel.vub.ac.be> writes:

Op 2005-11-03, Steven D''Aprano schreef <st***@REMOVETHIScyber.com.au>:

There are two possible fixes, either by prohibiting instance variables
with the same name as class variables, which would allow any reference
to an instance of the class assign/read the value of the variable. Or
to only allow class variables to be accessed via the class name itself.



There is also a third fix: understand Python''s OO model, especially
inheritance, so that normal behaviour no longer surprises you.



No matter wat the OO model is, I don''t think the following code
exhibits sane behaviour:

class A:
a = 1

b = A()
b.a += 2
print b.a
print A.a

Which results in

3
1



I find it confusing at first, but I do understand what happens :-)

But really, what should be done different here?

S.


这篇关于类可变访问和分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆