Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

What is the quick way of identifying unexpected identation error

 
Ranch Hand
Posts: 2424
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes while working Python interpretor says unexpected indentation whereas the code looks correct seeing the identation given inside the blocks. It seems to the eye spacing looks correct but something is there which python inetepretor takes as extra tabs.When I encounter such error, I revert the code changes using control z repeatedly and then again type the code and it works fine. While this works everytime for me ,is there a quick way of identifying.
Thanks.
 
Saloon Keeper
Posts: 23449
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the problem with interpreted languages like python. Bad code doesn't show up until something tries to compile it. Python's a really bad offender because if you print the code and examine it, bad use of tabs won't be detectable.

So the brute-force way to check bad indentation is to actually run the code.

If you can't do that or want something more refined, use a "lint" utility. Lint utilities, plus other useful code analysis tools for Python are listed here: https://realpython.com/python-code-quality/#linters

Some IDEs and text editors can be helpful in that they can be instructed to not use tab characters and/or to replace tabs with spaces.
 
Monica Shiralkar
Ranch Hand
Posts: 2424
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:
So the brute-force way to check bad indentation is to actually run the code.
.



After running the code, sometimes  identation looks correct to the eye but interpretor sees some tabs in identation which I can't. I have to revert code and add again which works. May be I will get used to and no more see such issue.
 
Tim Holloway
Saloon Keeper
Posts: 23449
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some editors have a "display blank characters" mode (like Microft Word). In that case, if you turn it on, spaces may show as dots and tabs might show as arrows.
 
Monica Shiralkar
Ranch Hand
Posts: 2424
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote: spaces may show as dots and tabs might show as arrows.


I am trying to understand that how are tabs different from space when on doing tab it creates space.
 
Ranch Foreman
Posts: 338
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I missed this.

I replied in another thread that when we say "never use tabs" we mean that make sure that whatever program you are editing Python code in is set to turn all tabs upon being typed into the appropriate number of space characters.

According to PEP 8, (I typed 808 somewhere because I'd been listening to Old School Hip-hop and typing too fast) that should be 4 characters.

It doesn't NEED to be 4, but it needs to be consistent.

I believe Python 2 used to be more flexible with guessing about mixed tabs and spaces for the nesting semantics, but Python 3 is very complain-y, which I consider a good thing.

The possibility of two screens of code looking identical to one's eye and having different meanings would be inviting disaster on any code that needs to work correctly.
 
Monica Shiralkar
Ranch Hand
Posts: 2424
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way I saw is to select the lines of code under the function in IDE like IntelliJ and click on tab.
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic