[Python-projects] pylint W0212 - Access to a protected member of a client class

Duncan Gibson duncan at thermal.esa.int
Tue Nov 14 10:23:52 CET 2006



If I run the following example Python script:
----------------------------------------------------------------------

#!/usr/bin/env python
''' example program to ask about pylint message W0212 '''

class Thing(object):
    ''' example class '''

    _next_id = 0

    __slots__ = ('number', 'name')

    def __init__(self, name):
        self.number = Thing.next_id()
        self.name   = name

    def __str__(self):
        return '(number=%d, name="%s")' % (self.number, self.name)

    def next_id():
        ''' static method to increment "private" class attribute '''
        Thing._next_id += 1
        return Thing._next_id
    next_id = staticmethod(next_id)

    def reset_id():
        ''' static method to reset "private" class attribute '''
        Thing._next_id = 0
    reset_id = staticmethod(reset_id)


if __name__ == '__main__':

    XX = Thing('one')
    print 'XX =', XX
    YY = Thing('two')
    print 'YY =', YY

    Thing.reset_id()
    ZZ = Thing('ten')
    print 'ZZ =', ZZ


then I get the following output, as expected:
----------------------------------------------------------------------

XX = (number=1, name="one")
YY = (number=2, name="two")
ZZ = (number=1, name="ten")


If I then run pylint on the script, I get two W0212 messages:
----------------------------------------------------------------------

W: 20:Thing.next_id: Access to a protected member _next_id of a client class
W: 21:Thing.next_id: Access to a protected member _next_id of a client class


So I ran pylint --help-message=W0212 to check the warning:
----------------------------------------------------------------------

:W0212: *Access to a protected member %s of a client class*
  Used when a protected member (i.e. class member with a name beginning
  with an underscore) is access outside the class or a descendant of the
  class where it's defined. This message belongs to the classes checker.


In my original code, I was accessing the "private" class attribute
directly from outside the class, received W0212 messages, and to avoid
the warnings I encapsulated the access in "public" static methods like
those above, and *still* got the warning messages.

And now that I've typed in all of this text, I realise that pylint gives
the warnings for accessing the private _next_id class attribute in the
next_id() static method, but not for accessing it in the reset_id() method!

What is the expected behaviour? Should both give warnings, or neither?


pylint --version gives:
----------------------------------------------------------------------

pylint 0.12.1, astng 0.16.1, common 0.19.2
Python 2.5 (r25:51908, Oct 11 2006, 16:04:20)
[GCC 3.4.6]


Cheers
Duncan

PS. It's not really too important, but all of the pylint warning messages
    that start "To many ..." should really read "Too many..." with two Os.
    English is tough stuff http://alt-usage-english.org/excerpts/fxenglis.html




More information about the Python-Projects mailing list