不清楚类变量 [英] Unclear On Class Variables

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

问题描述

我有点困惑。我的印象是:


class foo(对象):

x = 0

y = 1


表示x和y是一个类的所有实例共享的变量。

但是当我针对两个foo实例运行它时,设置值
$ b $ x和x,它们确实是*实例*的独特之处,而不是

类。


已经很晚了,我可能会失踪显而易见的。启蒙赞赏...

-

--------------------------- -------------------------------------------------

Tim Daneliuk tu****@tundraware.com

PGP Key : http://www.tundraware.com/PGP/

解决方案

Tim Daneliuk写道:

我有点困惑。我的印象是:

类foo(对象):
x = 0
y = 1

表示x和y是变量由类的所有实例共享。
但是当我针对foo的两个实例运行它,并设置x和y的值
时,它们确实是* instance *的唯一而不是
上课。

现在已经很晚了,我可能会错过这个显而易见的事。启蒙
赞赏...




如果没有实际代码如何设置变量,没有人可以回答这个问题。


-

问候,


Diez B. Roggisch


2005年1月13日美国东部时间07:18:26,Tim Daneliuk< tu **** @ tundraware.com>写道:

我有点困惑。我的印象是:

类foo(对象):
x = 0
y = 1

表示x和y是变量由类的所有实例共享。
但是当我针对foo的两个实例运行它,并设置x和y的值
时,它们确实是* instance *的唯一而不是
class。




我明白为什么你会这么想:

< blockquote class =post_quotes> class Spam(object):
.... eggs = 4

.... spam = Spam()
spam2 = Spam()
spam.eggs
4 spam2.eggs
4 spam.eggs = 2
spam.eggs
2 spam2.eggs
4


但是由于整数是不可变的,你被误导了。

''spam.eggs = 2''是*创建*实例成员 - 没有一个

之前。看看可变对象会发生什么:

类垃圾邮件(对象):
.... eggs = [3]

.... spam =垃圾邮件()
spam2 =垃圾邮件()
spam.eggs
[3] spam2.eggs
[3] spam.eggs.append(5)
spam.eggs
[3,5] spam2.eggs



[3,5]


-

干杯,

Simon B,
si *** @ brunningonline.net
http:// www。 brunningonline.net/simon/blog/


你好蒂姆,


如果你有
< br $>
类Foo(对象):

x = 0

y = 1


foo = Foo()


foo.x#读取实例或类属性(本例中为类)


foo.x = val#设置实例属性(因为foo实例不是

类)


Foo.x = val#设置一个类属性

foo .__ class .__ x = val#做同样的事情


这可能有时令人困惑。恕我直言,以下特别是

讨厌:

foo = Foo()
foo.x + = 1

打印foo.x
1打印Foo.x


0

虽然+ =运算符看起来像是在原地添加它不是。

它只是foo.x = foo.x + 1的语法糖。

- harold -

2005年1月13日07:18,Tim Daneliuk写道:

我有点困惑。我的印象是:

类foo(对象):
x = 0
y = 1

表示x和y是变量由类的所有实例共享。
但是当我针对foo的两个实例运行它,并设置x和y的值
时,它们确实是* instance *的唯一而不是
上课。

现在已经很晚了,我可能会错过这个显而易见的事。启蒙
赞赏......
-
------------------------------- ----------------------------------------
----- < Tim Daneliuk tu****@tundraware.com
PGP密钥: http://www.tundraware.com/PGP/
-
http://mail.python.org/mailman/listinfo/python-list



-

每个人都是天才。

只是有些人实现它太愚蠢了。


I am a bit confused. I was under the impression that:

class foo(object):
x = 0
y = 1

means that x and y are variables shared by all instances of a class.
But when I run this against two instances of foo, and set the values
of x and y, they are indeed unique to the *instance* rather than the
class.

It is late and I am probably missing the obvious. Enlightenment appreciated ...
--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/

解决方案

Tim Daneliuk wrote:

I am a bit confused. I was under the impression that:

class foo(object):
x = 0
y = 1

means that x and y are variables shared by all instances of a class.
But when I run this against two instances of foo, and set the values
of x and y, they are indeed unique to the *instance* rather than the
class.

It is late and I am probably missing the obvious. Enlightenment
appreciated ...



Without actual code how you set the vars, no one can answer that question.

--
Regards,

Diez B. Roggisch


On 13 Jan 2005 07:18:26 EST, Tim Daneliuk <tu****@tundraware.com> wrote:

I am a bit confused. I was under the impression that:

class foo(object):
x = 0
y = 1

means that x and y are variables shared by all instances of a class.
But when I run this against two instances of foo, and set the values
of x and y, they are indeed unique to the *instance* rather than the
class.



I can see why you might think that:

class Spam(object): .... eggs = 4
.... spam = Spam()
spam2 = Spam()
spam.eggs 4 spam2.eggs 4 spam.eggs = 2
spam.eggs 2 spam2.eggs 4

But you are being mislead by the fact that integers are immutable.
''spam.eggs = 2'' is *creating* an instance member - there wasn''t one
before. Have a look at what happens with a mutable object:
class Spam(object): .... eggs = [3]
.... spam = Spam()
spam2 = Spam()
spam.eggs [3] spam2.eggs [3] spam.eggs.append(5)
spam.eggs [3, 5] spam2.eggs


[3, 5]

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/


Hi Tim,

If you have

class Foo(object) :
x = 0
y = 1

foo = Foo()

foo.x # reads either instance or class attribute (class in this case)

foo.x = val # sets an instance attribute (because foo is instance not
class)

Foo.x = val # sets a class attribute
foo.__class.__x = val # does the same

this might be sometimes confusing. IMHO, the following is especially
nasty:

foo = Foo()
foo.x += 1

print foo.x 1 print Foo.x

0

although the += operator looks like an inplace add it isn''t.
it is just syntactic sugar for foo.x = foo.x + 1.
- harold -
On 13.01.2005, at 07:18, Tim Daneliuk wrote:
I am a bit confused. I was under the impression that:

class foo(object):
x = 0
y = 1

means that x and y are variables shared by all instances of a class.
But when I run this against two instances of foo, and set the values
of x and y, they are indeed unique to the *instance* rather than the
class.

It is late and I am probably missing the obvious. Enlightenment
appreciated ...
--
-----------------------------------------------------------------------
-----
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
--
http://mail.python.org/mailman/listinfo/python-list


--
Everyone is a genius.
It''s just that some people are too stupid to realize it.


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

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