是否有任何内置的Python值,其中道理的评价是无效的? [英] Is there any builtin Python value where truth evaluation is invalid?

查看:135
本文介绍了是否有任何内置的Python值,其中道理的评价是无效的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组子类,都应该定义属性的X 应该要么评估为真或假。为了赶上当我忘了在子类中设置这个值的错误,我想在它的超将其设置为一个值,而真理评估结果错误。有没有在Python这种行为的任何内置的价值呢?我有点期待 NotImplemented 有这种行为,但它的计算结果为

I have a set of sub-classes that should all define an attribute x that should either evaluate to True or False. In order to catch bugs when I forget to set this value in a subclass, I would like to set it in its superclass to a value for which truth evaluation results in an error. Is there any built-in value in Python with this behaviour? I sort of expected NotImplemented to have this behaviour, but it evaluates to True.

我可以将其设置为 numpy.array([0,0]),其中如果x 引发 ValueError错误,但感觉错了。同样,我可以定义自己的类,其中 __ __ BOOL 引发异常。但有任何内置值适合于这个目的?

I could set it to numpy.array([0, 0]) where if x: raises ValueError, but that feels wrong. Likewise, I could define my own class where __bool__ raises an exception. But is there any built-in value appropriate for this purpose?

其他选择将设置一个属性(抽象与否)或不都定义它(所以我们获得 AttributeError的)。

Other alternatives would be to set a property (abstract or not) or to not define it at all (so we get AttributeError).

(我使用Python 3.4,如果它事项)

(I'm using Python 3.4, in case it matters)

推荐答案

我最近遇到了同样的问题,有一个稍微不同的用例:

I recently encountered the same question, with a slightly different use case:

我有一类的标志属性,其价值是由主叫方传递给 __ __的init 。类的对象可以从两个不同版本的数据,其中数据的旧版本不包含需要确定标志是否应的信息来创建

I had a class with a flag attribute, the value of which is passed to __init__ by the caller. Objects of the class can be created from two different versions of data, where the older version of the data does not contain the information needed for determining whether the flag should be True of False.

原本布尔值设置为(这是重新presenting缺失数据的常用的方法)是行不通的,因为高高兴兴地计算结果为

Setting an otherwise bool-value to None (which is the common way of representing missing data) would not work, because None happily evaluates to False.

和你一样,我没有找到一个满意的内置解决方案,所以我写了一个自己。

Like you, I didn't find a satisfactory built-in solution, so I wrote one myself.

(书面的python2.7,但容易调整FO python3)

(Written for python2.7, but easy to adjust fo python3)

class NotTrueNorFalseType(object):
    """
    A singleton class whose instance can be used in place of True or False, to represent
    a value which has no true-value nor false-value, e.g. a boolean flag the value of
    which is unknown or undefined.
    """
    def __new__(cls, *args, **kwargs):
        # singleton
        try:
            obj = cls._obj
        except AttributeError:
            obj = object.__new__(cls, *args, **kwargs)
            cls._obj = obj
        return obj

    def __nonzero__(self):
        raise TypeError('%s: Value is neither True nor False' % self)

    def __repr__(self):
        return 'NotTrueNorFalse'

NotTrueNorFalse = NotTrueNorFalseType()

在这个类的设计(MIN-)的决定是由无启发单(例如命名一个实例富和类FooType,返回富从 __ __再版,募集类型错误无效操作)。

The design (min-)decisions in this class were inspired by the None singleton (E.g. naming the singleton instance "Foo" and the class "FooType", returning "Foo" from __repr__, raising TypeError on invalid operations).

这篇关于是否有任何内置的Python值,其中道理的评价是无效的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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