[Python-projects] when using defining-attr-method, method order matters

skip at pobox.com skip at pobox.com
Mon Apr 13 14:32:33 CEST 2009


Consider this simple class:

    class C:
        def __init__(self):
            self.x = 0
            self.reset()

        def set_y(self, y):
            self.y = y

        def set_x(self, x):
            self.x = x

        def reset(self):
            self.x = 0
            self.y = 0

If you run pylint over it like so:

    pylint --defining-attr-methods=__init__,reset resetwhen.py

it complains:

    resetwhen.py:9: [W, C.set_y] Attribute 'y' defined outside __init__

If, however, you reorganize it slightly so that reset() occurs before set_y,
pylint is happy.  I can understand why this error is emitted.  Pylint sees
the definition of self.y in set_y before it encounters reset and thus emits
the error message.

It seems to me that giving a bunch of attributes initial values is generally
of secondary importance to a class's functionality.  It should be ok to push
such methods to the end of the file.  I think pylint should collect the
location of attribute initialization as it runs and only report on
attributes which are not ever assigned in the methods specified by
defining-attr-methods, ignoring when they are encountered.

-- 
Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/
        "XML sucks, dictionaries rock" - Dave Beazley



More information about the Python-Projects mailing list