静态变量与类变量 [英] Static variable vs Class variable

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

问题描述

嗨。


我对差异以及如何定义静态和

类变量有疑问。 AFAIK,类方法是接收

类本身作为参数的方法,而静态方法是

与定义类静态运行的方法。


因此,我的理解是静态变量必须绑定到定义变量的

类,并由父类的子元素共享

其中变量已定义。但是,请看看这个代码在

中,一个人告诉我变量a是静态的:


>> class Foo:



a = 1

@classmethod

def增量(cls):

cls.a + = 1

print cls.a


这里,我定义变量a,我相信是类变量,

即不与Foo本身绑定的变量。相反,a必然是访问变量的类。验证这个想法的代码如下:


>>类Child1(Foo):



pass


>> Child1.increment()



4


>> class Child2(Foo):



pass


>> Child2.increment()



4


这意味着Child1和Child2不共享变量a

表示变量a是cla ss变量而不是静态变量。


你能评论一下吗?是静态变量还是类变量?

定义''class''和''static''变量的最新方法是什么?


谢谢。

- Minkoo

解决方案

mi ******** @ gmail.com 写道:


嗨。


我对差异以及如何定义静态和

类变量有疑问。 AFAIK,类方法是接收

类本身作为参数的方法,而静态方法是

与定义类静态运行的方法。


因此,我的理解是静态变量必须绑定到定义变量的

类,并由父类的子元素共享

其中变量已定义。但是,请看看这个代码在

中,一个人告诉我变量a是静态的:


>>> class Foo:



a = 1

@classmethod

def增量(cls):

cls.a + = 1

print cls.a



在你的increment()方法中,你这样做:


cls.a + = 1


它执行以下操作事情:


#1。阅读cls.a

#2。加一个

#3。将此值赋给cls.a


在#3点中,您确实将名称绑定到值。您可能知道,在
Python中,有名称和对象。名称''a''

的初始值为1.它是一个不可变对象。 + =运算符通常会递增对象的
值。但是,因为''int''类型是不可变的,所以+ =

运算符会将此变量重新绑定到新创建的值。我相信这就是这里发生的事情。你的问题是变量a

静态或类变量?没有真正的答案。在后代类上运行

increment()方法后,例如Child1将重新绑定

名称Child1.a,在类的名称空间中创建一个新名称。所以

变量Foo.a仍在那里,但你正在访问Child1.a。


如果你想把一个静态变量作为一个静态变量,你可以使用

静态方法来处理它。这不会在后代课程中绑定一个新名字。

(但是,你仍然可以重新绑定它,例如Child.a = 42)

现在这是一个很好的问题:如何从类(非静态)方法处理变量as static,

?这是一个例子:


>> class Foo(object):



.... a = 1

.... @classmethod

.... def增量(cls):

.... Foo.a + = 1

.... print cls.a

....


>> class Child1(Foo):



....传递

....


>> Child1.increment()



2


>> Child1.increment()



3


>> ; Foo.a



3

< blockquote class =post_quotes>
>> Child1.a = 10
Child1.increment()



10


>> Child1.increment()



10


>> Child1.increment()



10


>> Foo.a



6


>>>




然而,问题是:你为什么要这样做? :-)


BTW你应该尽可能使用新的风格类。旧式

课程将会消失...


希望这会有所帮助,


Laszlo


2007年10月9日星期二09:16:12 -0700, mi ******** @ gmail.com 写道:


我对差异以及如何定义有疑问static和

类变量。



首先你必须用静态来定义你的意思。


AFAIK,类方法是接收

类本身作为参数的那些,而静态方法是

与定义类静态运行的方法。



`classmethod` \s接收类作为第一个参数,`staticmethod` \s是

只是绑定到类的函数对象。


因此,我的理解是静态变量必须绑定到定义变量并由父类子元共享的



定义变量。但是,请看看这个代码在

中,一个人告诉我变量a是静态的:



问那个家伙是什么他的意思是静态。


>>> class Foo:



a = 1

@classmethod

def increment(cls):

cls.a + = 1

print cls.a


这里,我定义变量a,我相信是类变量,

即未绑定到Foo本身的变量。



不定义*属于*类'Foo`的类属性。


相反,a绑定到访问变量的类。证实这一想法的代码

如下:


>>> class Child1 (Foo):



pass


> ;>> Child1.increment()



4



四!?很难相信。


>>> class Child2(Foo):



pass


>>> Child2 .increment()



4


这意味着Child1和Child2不共享变量a这意味着

变量a是类变量而不是静态变量。


你能评论一下吗?是静态还是类变量?最新的定义''class''和''static''变量的方法是什么?



没有静态这样的东西变量。想想

绑定到对象的属性。全部动态。


结果是:将1绑定到`Foo`类

定义中的属性`Foo.a`。


当你调用`Child1.increment()`时,将使用

`Child1`作为第一个参数调用类方法。现在``cls.a + = 1``被执行,这是一个有点像`cls.a = cls.a + 1``的简短形式的
。所以这是从'Child1`读取$ a b $ b属性`a`然后将结果绑定到`Child1`。

`Child1`没有属性` a`,所以它在父母

类中查找。但结果却被绑定到了'Child1`。所以你正在阅读

`Foo`并写给'Child1`。就是这样。


Ciao,

Marc''BlackJack''Rintsch



您的问题是变量a

静态还是类变量?没有真正的答案。在后代类上运行

increment()方法后,例如Child1将重新绑定

名称Child1.a,在类的名称空间中创建一个新名称。所以

变量Foo.a仍然存在,但你正在访问Child1.a。



请注意,理论上有没有办法以任何方式更改价值

的Foo.a,因为这是一个引用

IMMUTABLE OBJECT的名称。这意味着您只能重新绑定变量。你的b $ b不能改变它的价值,因为当我们谈论它的价值时,我们

意味着所引用对象的状态。

名称引用的对象是整数实例,即它是一。对象一是指一。永远

仍然是一,这是无法改变的。你可以添加一对一,而你

将获得两个,但这是另一个对象。


(对不起,可能你已经知道了这个。)


Laszlo


Hi.

I''ve got a question on the differences and how to define static and
class variables. AFAIK, class methods are the ones which receives the
class itself as an argument, while static methods are the one which
runs statically with the defining class.

Hence, my understanding is that static variables must be bound to the
class defining the variables and shared by children of parent class
where the variable is defined. But, please have a look at this code in
which a guy told me that the variable a is static:

>>class Foo:

a = 1
@classmethod
def increment(cls):
cls.a += 1
print cls.a

Here, I am defining variable a which, I believe is class variable,
i.e., variable that is not bound to Foo itself. Rather, a is bound to
the class which is accessing the variable. The code that corroborates
this idea is as follows:

>>class Child1(Foo):

pass

>>Child1.increment()

4

>>class Child2(Foo):

pass

>>Child2.increment()

4

This means that Child1 and Child2 does not share variable a which
means that variable a is class variable rather than static variable.

Could you please comment on this? Is a static or class variable?
What''s the most recent way of defining ''class'' and ''static'' variables?

Thanks.
- Minkoo

解决方案

mi********@gmail.com wrote:

Hi.

I''ve got a question on the differences and how to define static and
class variables. AFAIK, class methods are the ones which receives the
class itself as an argument, while static methods are the one which
runs statically with the defining class.

Hence, my understanding is that static variables must be bound to the
class defining the variables and shared by children of parent class
where the variable is defined. But, please have a look at this code in
which a guy told me that the variable a is static:

>>>class Foo:

a = 1
@classmethod
def increment(cls):
cls.a += 1
print cls.a

In your increment() method, you do this:

cls.a += 1

It does the following thing:

#1. read cls.a
#2. add one
#3. assign this value to cls.a

In point #3, you really bind a name to a value. As you probably know, in
Python, there are names and objects. The initial value of the name ''a''
is 1. It is an immutable object. The "+=" operator usually increments a
value of an object. However, because the ''int'' type is immutable, the +=
operator will rather rebind this variable to a newly created value. I
believe this is what is happening here. Your question "is variable a
static or class variable?" has no real answer. After running the
increment() method on a descendant class, e.g. Child1 will rebind the
name Child1.a, creating a new name in the namespace of the class. So the
variable Foo.a is still there, but you are accessing Child1.a instead.

If you want to HANDLE a as a static variable, you can handle it with a
static method. That won''t bind a new name in the descendant class.
(However, you can still rebind it, e.g. "Child.a=42")
Now here is a good question: how do you handle a variable as static,
from a class (not static) method? Here is an example:

>>class Foo(object):

.... a = 1
.... @classmethod
.... def increment(cls):
.... Foo.a += 1
.... print cls.a
....

>>class Child1(Foo):

.... pass
....

>>Child1.increment()

2

>>Child1.increment()

3

>>Foo.a

3

>>Child1.a = 10
Child1.increment()

10

>>Child1.increment()

10

>>Child1.increment()

10

>>Foo.a

6

>>>



However, the question is: why would you do this? :-)

BTW you should use new style classes whenever it is possible. Old style
classes will have gone...

Hope this helps,

Laszlo


On Tue, 09 Oct 2007 09:16:12 -0700, mi********@gmail.com wrote:

I''ve got a question on the differences and how to define static and
class variables.

First you have to define what you mean by "static".

AFAIK, class methods are the ones which receives the
class itself as an argument, while static methods are the one which
runs statically with the defining class.

`classmethod`\s receive the class as first arguments, `staticmethod`\s are
just functions bound to the class object.

Hence, my understanding is that static variables must be bound to the
class defining the variables and shared by children of parent class
where the variable is defined. But, please have a look at this code in
which a guy told me that the variable a is static:

Ask the guy what he means by "static".

>>>class Foo:

a = 1
@classmethod
def increment(cls):
cls.a += 1
print cls.a

Here, I am defining variable a which, I believe is class variable,
i.e., variable that is not bound to Foo itself.

No you define a class attribute that *is* bound to the class `Foo`.

Rather, a is bound to the class which is accessing the variable. The code
that corroborates this idea is as follows:

>>>class Child1(Foo):

pass

>>>Child1.increment()

4

Four!? Hard to believe.

>>>class Child2(Foo):

pass

>>>Child2.increment()

4

This means that Child1 and Child2 does not share variable a which means
that variable a is class variable rather than static variable.

Could you please comment on this? Is a static or class variable? What''s
the most recent way of defining ''class'' and ''static'' variables?

There is no such thing as a "static" variable. Think of attributes that
are bound to objects. All dynamically.

What happens is: you bind a 1 to the attribute `Foo.a` in the `Foo` class
definition.

When you call `Child1.increment()` the class method will be called with
`Child1` as first argument. Now ``cls.a += 1`` is executed which is
somewhat like a short form of ``cls.a = cls.a + 1``. So this is reading
the attribute `a` from `Child1` and then bind the result to `Child1`.
`Child1` doesn''t have an attribute `a`, so it is looked up in the parent
class. But the result is then bound to `Child1`. So you are reading from
`Foo` and writing to `Child1`. That''s it.

Ciao,
Marc ''BlackJack'' Rintsch


Your question "is variable a
static or class variable?" has no real answer. After running the
increment() method on a descendant class, e.g. Child1 will rebind the
name Child1.a, creating a new name in the namespace of the class. So the
variable Foo.a is still there, but you are accessing Child1.a instead.

Please notice, that theoretically there is no way to "change the value"
of Foo.a in any way, because this is a NAME that references to an
IMMUTABLE OBJECT. It means that you can only rebind the variable. You
cannot change its value, because when we are talking about its value, we
mean the state of the referenced object. The object referenced by the
name is an integer instance, namely it is "one". The object "one" always
remains "one", this cannot be changed. You can add one to one, and you
will get two, but that is another object.

(I''m sorry, probably you already knew this.)

Laszlo


这篇关于静态变量与类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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