how to format a python source file with tools?

Diez B. Roggisch deets at
Fri Nov 27 16:58:01 CET 2009

李白,字一日 schrieb:
> On Nov 27, 3:35 pm, Ben Finney <ben+pyt... at> wrote:
>> 李白,字一日 <calid... at> writes:
>>> or is it possible for large source files?
>> Is what possible? What do you want the tool to do?
>> --
>>  \           “I do not believe in forgiveness as it is preached by the |
>>   `\        church. We do not need the forgiveness of God, but of each |
>> _o__)                    other and of ourselves.” —Robert G. Ingersoll |
>> Ben Finney
> sometimes i need to merge some code snippets from files into a file,
> and when time comes to do this merge, i always find it difficult to
> reformat the python code
> because after pasting and copy, the code indentation different from
> one to another.
> and it is the tedious job for us to manually move the code from one
> segment to another.
> so i would like to have a tool to intelligently format the code for me
> and make the code more beautiful
> and automated.

This is not possible. Consider the following situation:

code a:

if something:

code b:

if anything:

No lets say you copy b after a. At which level should it be? Like this

if something:

if anything:

Or like this?

if something:

    if anything:

Both are semantically radically different, and only you know which one 
is the right one.

What my editor (emacs) allows, and other IDEs/editors as well is to mark 
blocks, and shift these wholesome left or right, to get the desired 
indention level.

The only thing that migh be automatized after a piece of code is valid 
is normalization, like de-tabifying or making everything based on 4 
space characters indention. No idea if there is something out there that 
does that.


More information about the Python-list mailing list