0

okay, I thought such code could not get wrong, but it obviously does:

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))

I'm just trying to initialise a [min,max] range with some class hierarchy. But for some totally odd reason, p=0.888337 will raise the following exception:

    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.

Has anybody any clue about what's happening ? I have to admit I'm far from mastering the Python language, but I fail to see any subtlety that could explain such an odd behaviour.

PypeBros
  • 2,607
  • 24
  • 37

1 Answers1

2

ah. Figured out.

  • missing a self in __init__ call to the superclass
  • 1.000 'extra value' is a True from the ... trans-typed to float

so superclass constructor invocation 'breaks' the call model and require explicit self reference, huh ?

PypeBros
  • 2,607
  • 24
  • 37
  • I suppose it'd have been easier to debug if I had any use of self.__something before I do the check and raise the error ... – PypeBros May 10 '12 at 15:16
  • 1
    It doesn't break the call model. Python methods get the instance as the first argument when called on an instance. But since you are calling from the class, you have to pass the instance explicitly. If that seems odd to you, you could use 'super' instead. – XORcist May 10 '12 at 17:09
  • as in http://stackoverflow.com/questions/576169/understanding-python-super-and-init-methods, thus ... – PypeBros May 11 '12 at 08:17