Search code examples
pythoninitializationpylint

Using unitialized variables in Python


Background: I have a class modeling a chip with registers, the chip has a bunch of registers, one of them is a high temperature limit for the built-in temperature sensor.

I have the following:

class foo():
  def __init__(self):
    # does not set self._hiTemp!
    ...
  def setHiTemp(self, t):
    self._hiTemp = t
  def getHiTemp(self):
    return self._hiTemp
  def checkHiTemp(self):
    return self._temp > self._hiTemp

The reason that I don't declare self._hiTemp in the __init__ is because the user may not care about the temp-sensing capabilities of the chip. The user can be using the chip in different ways, and there is no meaning in giving this variable a meaningless value. However, if the user tries to use self._hiTemp without first setting it, the error of using undeclared variables is much easier to debug/backtrace than some obscure error like comparing numbers and None (or in some case even no errors at all).

This is all going fine until I start pylint, and of course I get W0201: Attribute defined outside init just about everywhere. I'm just wondering if this style of coding is frowned upon, and if so, what the "Pythonic way" is.

Thanks


Solution

  • The way I'd do it is set it to None or some other sentinel value that doesn't occur 'in nature'. Then for operations that require it to be set, use an assert to fail fast in case the caller is trying to use your object inappropriately.

    def __init__(self):
        self._hiTemp = None
    
    def checkHiTemp(self):
        assert self._hiTemp is not None, 'Why you no set _hiTemp before checking it?'
        return self._temp > self._hiTemp