newbie question

Peter Hansen peter at engcorp.com
Wed Feb 9 18:40:27 EST 2005


doodle4 at gmail.com wrote:
> Thanks for the reply.
> 
> I am trying to convert some C code to python and i was not sure what
> the equivalent python code would be.
> 
> I want to postdecrement the value in the while loop. Since i cannot use
> assignment in while statements is there any other way to do it in
> python?

Generally in Python you simply put the assignment-based
statement in a "while True:" block, and use "break" to
exit as required.  For example, here is one way to
approach it in this case:

while True:
     n = n - 1
     if n == -1:
         break

Having written the above, however, I'm uncertain whether this
sort of thing is really required in Python.  Although I have
ported very little C code to Python (so I might be wrong), I
definitely have never seen anything resembling the above
pattern in Python code that I or others have written.

My suspicion is that the code involves operations which are
actually better handled in Python by some kind of builtin
or standard library operation.  For example, often the
post-decrementing is part of a pointer operation involving
manipulating a string character-by-character.  This is
effectively never required in Python, and will pretty much
always result in a program written at a much lower level
than what you should be shooting for in Python.  But if
you want a direct port, then the above should do...

-Peter



More information about the Python-list mailing list