Why do Pythoneers reinvent the wheel?

Magnus Lycka lycka at carmen.se
Wed Sep 14 07:50:07 EDT 2005


Claudio Grondi wrote:
> To name a simplest example:
> What should I do to find a piece of code taking an
> integer and giving a string with binary form of a
> number? How to put some available pieces of code
> together if the binary form is needed and the integer
> is provided as a string holding its hexadecimal form?
> What if the string is the binary representation of the
> integer value as internally stored in memory?
> What if I would like the binary form to be splitted
> in nibbles separated with one space and bytes with
> two spaces?

It's possible that you have a point in principle,
but these examples don't strengthen your point.

A function that turns e.g. 5 into '101' is trivial
and just a few lines of code. Finding that in some
kind of code catalog would certainly be more work
than to just code it. Besides, there are a number
of variants here, so a variant that makes everybody
happy when it concerns dealing with negative numbers,
range checks, possibly filling with zeros to a certain
length etc, would probably be both bigger and slower
than what the average Joe needs.

This is simply the wrong level of reuse. It's too
simple and too varied. To be able to express things
like that in code is very basic programing.

You create integers from numeric representation in
strings with the int() function. You should read
chapter 2 in the library reference again Claudio.
This is one of the most common builtin function.
int() accepts all bases you are likely to use and
then some.

Filtering out spaces is again trivial.
"0101 0110".replace(' ','') Also chapter 2 in the
library manual.

You should read this until you know it Claudio! It's
really one of the most important pieces of Python
documentation.

I might be wrong, but I suspect you just need to get
more routine in programming. Your reasoning sounds a
bit like: "I don't want to invent new sentences all the
time, there should be a catalog of useful sentences
that I can look up and use.

Sure, there are phrase books for tourists, but they are
only really interesting for people who use a language
on a very naive level. We certainly reuse words, and
it's also very useful to reuse complete texts, from
short poems to big books. Sure, many sentences are often
repeated, but the ability to create new sentences in
a natural language is considered a basic skill of the
user. No experienced user of a language use phrase
books, and if you really want to learn a language
properly, phrase books aren't nearly as useful as proper
texts.

There are simply so many possibly useful sentences, so
it would be much, much more work to try to catalog and
identify useful sentences than to reinvent them as we
need them.

It's just the same with the kinds of problems you described
above. With fundamental language skills, you'll solve these
problems much faster than you can look them up. Sure, the
first attempts might be less than ideal, especially if you
haven't read chapter 2 in the library manual, but you learn
much, much more from coding than from looking at code
snippets.



More information about the Python-list mailing list