The Python Way

Tim Peters tim.one at comcast.net
Wed Mar 27 23:06:49 EST 2002


[Guido]
> "The joy of coding Python should be in seeing short, concise, readable
> classes that express a lot of action in a small amount of clear
> code -- not in reams of trivial code that bores the reader to death."

[Dale Strickland-Clark]
> I didn't see the original post but I *really* hope this was said in
> jest.
>
> Or is Guido getting writing software and writing novels confused?

[Aahz]
> Not in the slightest.  Guido was responding to constructs like this:
>
>     lock.acquire()
>     try:
>         code
>         code
>         code
>         code
>         code
>         code
>         code
>         code
>         code
>         code
>         code
>         code
>     finally:
>         lock.release()

Actually, he wasn't.  That was my attempt to hijack the favorable mindshare
Guido attracted toward one of my own pet peeves.  What Guido was actually
railing against is code like this:

class XandY:
    """A class to wrap x and y values.

    Use the .get_x() method to get the x value.
    Use the .set_x() method to set the x value.
    Use the .get_y() method to get the y value.
    Use the .set_y() method to g^Hset the y value.
    """

    x = None   # default value for x
    y = None   # default value for y

    def __init__(self, x=None, y=None):
        self.set_x(x)   # set the x value
        self.set_y(y)   # set the y value

    def get_x(self):
        "Return the current x value, as most recently set by .set_x()."
        return self.x

    def get_y(self):
        "Return the current y value, as most recently set by .set_y()."
        return self.y

    def set_x(self, xvalue):
        """Set the current x value to xvalue.

        .get_x() returns xvalue if no other call to .set_x intervenes.
        """

        self.x = xvalue

    def set_y(self, xvalue):
        """Set the current y value to yvalue.

        .get_y() returns yvalue if no other call to .set_y intervenes.
        """

        self.y = yvalue

    def sum(self):
        "Return the sum of x and y."
        return self.x + self.y

followed by 50 lines of unit tests to verify proper operation of the trivial
methods.  This kind of numbing excess is self-inflicted (for a meaning of
"self" broad enough to encompass one's employer <wink>).





More information about the Python-list mailing list