running python 2 vs 3

Chris Angelico rosuav at gmail.com
Thu Mar 20 22:18:29 EDT 2014


On Fri, Mar 21, 2014 at 12:06 PM, Roy Smith <roy at panix.com> wrote:
> In article <532b8f0d$0$29994$c3e8da3$5496439d at news.astraweb.com>,
>  Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
>
>> The rule of three applies here: anything you do in three different places
>> ought to be managed by a function.
>
> I prefer the rule of two :-)

The way I explain it is: Three is a rule of thumb. Sometimes it's
blatantly obvious at two, and other times you need four or five
similar pieces of code before you can see which part should become the
function. If the code's absolutely identical and reasonably
long/complex, then yes, two's all you need, but how often is that?
Usually it's similar, rather than congruent... err I mean identical.
That's where the third usage comes in. Or if it's maybe 2-3 lines,
used in two places, it doesn't necessarily need to be a function.
Again, a third usage is a strong hint that it should be broken out.

The rule doesn't say that anything that *isn't* in three places yet
should *not* be broken out. :)

ChrisA



More information about the Python-list mailing list