当前位置: 首页 > 面试题库 >

如何强制/确保类属性是特定类型?

归和惬
2023-03-14
问题内容

如何在Python中将类成员变量限制为特定类型?

较长版本:

我有一个具有多个成员变量的类,这些成员变量在该类的外部设置。由于它们的使用方式,它们必须为特定类型,即int或list。

如果这是C ++,则只需将它们设为私有,然后在“
set”函数中进行类型检查。鉴于这是不可能的,是否有任何方法可以限制变量的类型,以便在为其分配了错误类型的值时在运行时发生错误/异常?还是我需要在使用它们的每个函数中检查它们的类型?


问题答案:

您可以像其他答案一样使用属性-因此,如果您想约束单个属性,例如说“ bar”,并将其约束为整数,则可以编写如下代码

class Foo(object):
    def _get_bar(self):
        return self.__bar
    def _set_bar(self, value):
        if not isinstance(value, int):
            raise TypeError("bar must be set to an integer")
        self.__bar = value
    bar = property(_get_bar, _set_bar)

这有效:

>>> f = Foo()
>>> f.bar = 3
>>> f.bar
3
>>> f.bar = "three"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in _set_bar
TypeError: bar must be set to an integer
>>>

(还有一种新的属性编写方法,使用内置的“属性”作为getter方法的装饰器-但我更喜欢旧方法,就像我上面提到的那样)。

当然,如果您的类具有很多属性,并希望以这种方式保护所有属性,那么它就会变得冗长。不用担心-
Python的自省功能允许创建一个类装饰器,该装饰器可以用最少的行自动实现。

def getter_setter_gen(name, type_):
    def getter(self):
        return getattr(self, "__" + name)
    def setter(self, value):
        if not isinstance(value, type_):
            raise TypeError(f"{name} attribute must be set to an instance of {type_}")
        setattr(self, "__" + name, value)
    return property(getter, setter)

def auto_attr_check(cls):
    new_dct = {}
    for key, value in cls.__dict__.items():
        if isinstance(value, type):
            value = getter_setter_gen(key, value)
        new_dct[key] = value
    # Creates a new class, using the modified dictionary as the class dict:
    return type(cls)(cls.__name__, cls.__bases__, new_dct)

然后,您仅用auto_attr_check作类装饰器,并在类主体中声明要与属性也需要约束的类型相等的属性:

...     
... @auto_attr_check
... class Foo(object):
...     bar = int
...     baz = str
...     bam = float
... 
>>> f = Foo()
>>> f.bar = 5; f.baz = "hello"; f.bam = 5.0
>>> f.bar = "hello"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in setter
TypeError: bar attribute must be set to an instance of <type 'int'>
>>> f.baz = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in setter
TypeError: baz attribute must be set to an instance of <type 'str'>
>>> f.bam = 3 + 2j
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in setter
TypeError: bam attribute must be set to an instance of <type 'float'>
>>>


 类似资料:
  • 我有一份抽象的课堂报告: 我需要强制所有子类填充此 surrogateId 属性。(如果子类未填充属性,则会出现编译错误) 我尝试使用 final 关键字来强制我在构造函数中引入此属性的值,但我必须手动生成构造函数而不是使用 Lombok,很多样板代码。有没有办法在不手动生成结构的情况下实现相同的期望。

  • 我正在尝试更新当前登录用户的密码。但它显示了零点异常。 配置文件控制器: ProfileService: 给我这个错误: 2019-07-09 01:55:04.284错误---[nio-8080-exec-8]o.g.web.errors.GrailsExceptionResolver: NullPointerExcture发生时处理请求:[POST] /profile/doPasswordCh

  • 问题内容: 如何检查seleniumWeb元素是否包含特定的CSS类。 我有这个html li元素 如您所见,在class属性内部有一个活动类。 我的问题是我有这个元素,我想根据class属性是否具有其他“ active”值进行检查,这是比使用xpath更优雅的解决方案。 我怎样才能做到这一点? 问题答案: 鉴于您已经找到了您的元素,并且您想检查class属性内的某个类: 正如@aurelius正

  • 我正在用Python制作一个基于文本的RPG,并尝试设置地图和玩家移动。然而,我还没有弄清楚如何“链接”房间,以便玩家可以使用诸如北、南等命令从一个房间无缝移动到另一个房间。 我正在使用一个名为“Room”的类,它具有x坐标和y坐标属性。我已经制作了地图中每个“瓷砖”的实例,具有特定的x-pos和y-pos值。我要做的是根据当前的x-pos或y-pos是什么,转到“Room”类的当前实例。但是,我

  • 我正在建立一个本体论。 我有一个叫的类 我有一个名为的对象属性 我添加了一个相当于的

  • 我有如下注释的类: 从这个意义上说,我希望标识类并为它们创建对象,并将它们放在的一般中。 我如何使用反射和Java来做到这一点?