Python arrays and sting formatting options

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Oct 2 19:49:16 EDT 2008


On Thu, 02 Oct 2008 14:51:29 +0000, Steven D'Aprano wrote:

> On Wed, 01 Oct 2008 10:38:12 +0000, Marc 'BlackJack' Rintsch wrote:
> 
>> Even if newbies don't understand all the details they should be
>> introduced to ``with`` right away IMHO.  Because if you explain all the
>> details, even if they understand them, they likely will ignore the
>> knowledge because doing it right is a lot of boiler plate code.  So
>> usually people write less robust code and ``with`` is a simple way to
>> solve that problem.
> 
> So what you're saying is that we should encourage cargo-cult coding.
> "Write this boilerplate, because I tell you that if you do, good things
> will happen."

It's not cargo cult programming if you tell people to use the ``with`` 
statement to make sure the file will be closed after the block is left, 
for whatever reason the block was left.

>> Why on earth has everything to be guessable for someone who doesn't
>> know Python or even programming at all?
> 
> Oh please. Don't take my words out of context. I'm not talking about
> "everything", and I'm not suggesting that advanced programming features
> should be prohibited and we should write to the level my grandmother
> would understand.
> 
> The context was that a Fortran programmer asked for some help in writing
> a piece of code in Python. Your answer was entirely opaque and
> undecipherable to the OP. If your intention in answering was to teach
> the OP how to write Python code, you failed, because the OP couldn't
> understand your code! You can argue with me until Doomsday and it won't
> change that basic fact.

My intention wasn't to teach the OP how to write Python but to give a 
concise, easy and straight forward solution in Python.  Yes, I really 
believe I have written such thing.  I'm well aware that a Fortran 
programmer will not understand this without learning Python.

> Your answer may have solved the OP's *technical* problem, but it didn't
> do anything to solve the OP's *actual* problem, which was that he didn't
> know enough basic Python techniques to solve a simple problem. And
> that's the issue I was commenting on.

If he doesn't know enough basic Python techniques to solve *a simple 
problem* I think this is the wrong forum and he should work through the 
tutorial from the documentation to learn the basics first.  The tutorial 
includes `map()`, list comprehensions, methods in strings, the fact that 
files are iterable, and generator expressions.

> [more snippage]
>> > Nevertheless, for people coming from less dynamic languages than
>> > Python (such as Fortran), it is a common idiom to never use the same
>> > variable for two different things.  It's not a bad choice really:
>> > imagine reading a function where the name "lines" started off as an
>> > integer number of lines, then became a template string, then was used
>> > for a list of character positions...
>> 
>> Which I'm not doing at all.  It has the same duck type all the time:
>> "iterable of lines".
> 
> It has nothing to do with duck typing and everything to do with re-use
> of variables (or in Python, names) for different "things". Just because
> "lines" has the same duck-type doesn't mean they are conceptually the
> same things.

Of course it means they are the same "things", that is what duck typing 
is about.  In a statically typed language `lines` would be declared as 
`Iterable<str>` or similar.  Files opened for reading have that interface 
and the generator expression has the very same type.  A hypothetically 
statically typed Python variant with a ``declare`` statement should 
compile the following without problems because `generator` objects would 
implement `Iterable<A>` and `line` is of type `str`:

declare lines as Iterable<str>
lines = open('test.txt')
lines = (line for line in lines if line.strip())
#...

>> *I* think I would do Python a disservice if I encourage people to
>> continue writing Python code as if it where language X or pretending
>> Python is all about "readable, executable Pseudocode for anyone".
> 
> There's no "pretending". Python is excellent for writing readable,
> executable pseudo-code for anyone.

Yes, but that's not what Python is all about.  I use it for programming 
and not for writing code with the primary goal to be newbie friendly or 
pseudo code like.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list