"Python如何处理我的构造函数参数?"

2024-09-28 01:30:12 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我以为这样的代码不会出错,但显然是这样的:

somewhere:
    # p is a float value between 0 and 1
    m.limit=PidRange(p-1.0, p+1.0)

class PidRange(Range):
    def __init__(self, low, up):
        Range.__init__(low,up,...)
        pass
    # some methods definition for the PidRange sub-class

class Range(object):
    def __init__(self, p_min=None, p_max=None, ...):
        if (p_min > p_max):
             raise ValueError("Range can't be created: the low bound %f exceeds high bound %f."%(p_min,p_max))

我只是想用一些类层次结构初始化一个[min,max]范围。但由于一些完全奇怪的原因,p=0.888337会 引发以下异常:

    File "src/__main__.py", line 155, in __find_data
        m.limit=PidRange(p-1.0, p+1.0)
    File "src/routing.py", line 32, in __init__
       Range.__init__(low, up, low!=None, up!=None)
    File "src/equation.py", line 30, in __init__
       raise ValueError("Range can't be created: the low bound %f exceeds high bound %f."%(p_min,p_max))
    ValueError: Range can't be created: the low bound 1.888337 exceeds high bound 1.000000.

有人知道发生了什么事吗?我不得不承认,我还远远没有掌握Python语言,但我看不出任何微妙之处可以解释这种奇怪的行为。你知道吗


Tags: thenoneinitrangebemincanmax

热门问题