gpt4 book ai didi

python - 限制类变量的修改,新实例除外

转载 作者:太空狗 更新时间:2023-10-30 01:56:43 27 4
gpt4 key购买 nike

这是对以下帖子的跟进问题(理解问题不需要检查链接)

Counter variable for class

我们将 idCounter 设置为 Student 类的类变量,它计算创建的实例数。

这是类:

class Student:
# A student ID counter
idCounter = 0
def __init__(self):
self.gpa = 0
self.record = {}
# Each time I create a new student, the idCounter increment
Student.idCounter += 1
self.name = 'Student {0}'.format(Student.idCounter)

现在,我们实例化几个实例,然后检查 idCounter 的值:

student1 = Student()
student2 = Student()
student3 = Student()
student4 = Student()

Student.idCounter
4

但是,如果您可以这样做,那么维护一个计数器就没有实际意义了:

Student.idCounter = 2000

现在创建新实例:

student5 = Student()

并检查idCounter:

Student.idCounter

2001

idCounter 可以简单地搞砸计数器而无需运行 __init__

如何创建一个只在 __init__ 运行时递增的计数器(或任何类变量)?并且不能通过从类中调用类变量来独立修改,如上所示。

是否有通用的方法来限制使用语法修改类变量?

ClassName.ClassVariable = new_value

谢谢。

最佳答案

编辑

具有属性但原理相同的改进版本:

class Meta(type):

def __init__(cls, *args, **kwargs):
cls.__value = 0
super().__init__(*args, **kwargs)

@property
def idCounter(cls):
return cls.__value

class Student(metaclass=Meta):

def __init__(self):
self.__class__._Meta__value += 1

现在:

>>> s1 = Student()
>>> Student.idCounter
1
>>> s2 = Student()
>>> Student.idCounter
2
>>> Student.idCounter = 100
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-64-a525899df18d> in <module>()
----> 1 Student.idCounter = 100

AttributeError: can't set attribute

旧版本

使用描述符和元类:

class Counter:
def __init__(self):
self.value = 0
def __get__(self, instance, cls):
return getattr(instance, '_{}__hidden_counter'.format(instance.__name__ ))
def __set__(self, instance, value):
raise NotImplementedError

class Meta(type):
idCounter = Counter()

class Student(metaclass=Meta):
__hidden_counter = 0

def __init__(self):
Student.__hidden_counter += 1

似乎实现了这一点:

>>> s1 = Student()
>>> Student.idCounter
1
>>> s2 = Student()
>>> Student.idCounter
2
>>> Student.idCounter = 200
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-51-dc2483b583f6> in <module>()
----> 1 Student.idCounter = 200

<ipython-input-46-b21e03bf3cb3> in __set__(self, instance, value)
5 return getattr(instance, '_{}__hidden_counter'.format(instance.__name__ ))
6 def __set__(self, instance, value):
----> 7 raise NotImplementedError
8
9 class Meta(type):

NotImplementedError:
>>> Student.idCounter
2

这仍然可以被故意破坏:

>>> Student._Student__hidden_counter = 100
>>> Student.idCounter
100

但并非偶然。

关于python - 限制类变量的修改,新实例除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48699529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com