Is there an easy way to control indents in Python

Chris “Kwpolska” Warrick kwpolska at gmail.com
Wed Oct 15 10:32:28 EDT 2014


On Wed, Oct 15, 2014 at 9:27 AM, alex23 <wuwei23 at gmail.com> wrote:
> On 15/10/2014 12:23 PM, Juan Christian wrote:
>>
>> Using PyCharm is easy:
>>
>> File > Settings > (IDE Settings) Editor > Smart Keys > Reformat on paste
>>  > choose "Reformat Block"
>
>
>
> This isn't as straight forward as you imply. Say I have misindented code
> like this:
>
>     if True:
>     print 'true'
>     else:
>     print 'false'
>     print 'done'
>
> If I select this block in PyCharm and reformat it, I get:
>
>     if True:
>         print 'true'
>     else:
>     print 'false'
>     print 'done'
>
> Which is still invalid. Even if it did work more fully, though, how would it
> determine the correct placement of the last line of code?
> --
> https://mail.python.org/mailman/listinfo/python-list

It should parse this as

else:
    print 'false'
    print 'done'

Why?  Because things like `print 'done'` usually have an empty line before it:

if True:
print 'true'
else:
print 'false'

print 'done'

That should be parsed the way you want it done.  Makes perfect sense
when you look at it.

-- 
Chris “Kwpolska” Warrick <http://chriswarrick.com/>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense



More information about the Python-list mailing list