类变量在对象之间是否通用

问题描述 投票:0回答:1

假设我想使用类变量来实现对象之间的公共变量(类似于java/c++中的static)。

当我使用对象访问类变量时,它显示默认值。 然后我通过对象更新了类变量,它没有更新其他对象的类变量。 当通过其他对象或类名直接访问类变量时,它显示旧值。

为什么会这样,Python中如何制作类/静态变量(对象之间通用)

# Class for Computer Science Student
class CSStudent:
    
    stream = 'cse'               # Class Variable
    def __init__(self,name):
        self.name = name     # Instance Variable

# Objects of CSStudent class
a = CSStudent('Geek')
b = CSStudent('Nerd')


print(a.stream) # prints "cse"
print(b.stream) # prints "cse"
print(a.name) # prints "Geek"
print(b.name) # prints "Nerd"

# Class variables can be accessed using class
# name also
print(CSStudent.stream) # prints "cse"

# Now if we change the stream for just a it won't be changed for b
a.stream = 'ece'
b.stream = 'abc'
print(CSStudent.stream)  # prints 'ece'
print(a.stream) # prints 'ece'
print(b.stream) # prints 'abc'
python class static-variables
1个回答
0
投票

您需要了解查找程序。

首先,实例和类都有自己的命名空间。只有类变量在所有实例之间共享。该命名空间可通过

__dict__
属性访问,

当你从一个对象访问一个属性时,Python首先查看该对象的命名空间,如果能找到它,就返回它,否则它会在它的类中找到它!

class CSStudent:
    stream = "cse"

a = CSStudent()
b = CSStudent()
print("stream" in a.__dict__)          # False
print("stream" in b.__dict__)          # False
print("stream" in CSStudent.__dict__)  # True

print(a.stream)  # cse
print(b.stream)  # cse

所以

a
b
没有那个
stream
,只有班级有。

现在

a.stream = "something"
会将该属性添加到该特定实例的命名空间中。

a.stream = "something"
print("stream" in a.__dict__)          # True
print("stream" in b.__dict__)          # false
print("stream" in CSStudent.__dict__)  # True

现在,如果你访问

a.stream
,它会在
a
的命名空间中找到它并返回它,但因为
b
没有这个,它会在类中找到它。(共享的)

注意:我通过没有提及什么是描述符来简化解释。稍后再看吧。

© www.soinside.com 2019 - 2024. All rights reserved.