[Python-Dev] PEP 8 updates/clarifications

skip@pobox.com skip at pobox.com
Tue Dec 13 05:38:26 CET 2005


    Jim> I don't understand this argument.  Any mutating method or property
    Jim> invoked by foreign code changes an object's state.  

Sure, but the only place I need to look for direct changes to the object's
state are in the object's own code.

    Jim> If you provide a property or a pair if accessors that just sets and
    Jim> gets an attribute with a slightly different name, that affords no
    Jim> more protection than if people were setting the attribute directly.

Sure it does.  Suppose I get an exception in my code because some bit of
code somewhere broke my assumptions about the values an attribute could
assume.  If that attribute is only set by the object's own code, I can more
easily debug it (stick in a print or an assert in the places where the
attribute changes, etc).  If some external bit of code does something like

    self.foo = Foo()
    ...
    self.foo.attr = None

then later in Foo's code I have something like

    self.attr.callme()

The first thing I need to do is figure out who stomped on self.attr.  That
can be time-consuming if I don't necessarily know where the stomping
occurred.

At work we use Python for very rapid development of trading applications.
Today I think we made about a half-dozen micro releases fixing bugs and our
traders tried each one immediately live.  Much of the design is carried
around in our heads or consists of a few equations scribbled on sheets of
paper.  As you might imagine, it's a very lively environment.  Localizing
attribute modifications is a very good thing for us, even if they are simply
one-line set methods.

    Jim> If you don't want external code to change an attribute, don't
    Jim> expose it through a public API.

I suppose "public" is subject to some interpretation.  Just because I don't
prefix an attribute with an underscore doesn't mean I've implicitly declared
it public.  I assume that people will familiarize themselves with the
callable methods of an object and only use direct attribute access if I
haven't provided the necessary methods.

Skip


More information about the Python-Dev mailing list