Python Forum
Making the tab key work like a tab key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making the tab key work like a tab key
#1
Folks,

Is there a way to make the tab key (on the keyboard) work like a tab key? I like to use it to tab over 8 spaces or so per key-hit to align my comments. It seems though that if my preferences are set to "tab always indent" that a tab causes the line of code to indent. If that is not set a single tab before a comment (a comment after a line of code) indents the comment one space, a second tab calls up a context-sensitive menu.

And (strangely to me) if "tab always indent" is set, the line of code that is indented then gets the error message "unexpected indent". If I delete the tab and use two spaces, the error goes away.

I know I'm missing something basic.

Jack
Reply
#2
What the tab key does is application specific. What application are you talking about?
Reply
#3
Sorry, Spyder with Python.
Reply
#4
Your tab characters may be wrong. For Python the tab characters should be 4 spaces, not 2 or 8. Avoid using the tab character for tabs unless working on existing code that used tabs for indenting. If you mix tabs and spaces for indenting, you get that "unexpected indent" error. Of course you can also get the unexpected indent error for having unexpected indenting. Indenting in python is like brackets in C++. It is used to block code, not to make code pretty (that is a side effect).

Sounds like you break some PEP8 coding conventions.

https://peps.python.org/pep-0008/

Inline comments should be used sparingly. Block comments should be indented to the same level of the code. If you are trying to make inline comments line up like a block comment, ask yourself why you think you need to punish yourself.
def a_descriptive_function_name():
    """Use a docstring here to describe what the function does."""
    ...understandable code...

    # Use a block comment if additional info is required to
    # understand code.  Strive to make code easy to understand
    # so few comments are required.
    ...complicated code that needs additional description.

    ... code ...  # Don't using inline comments in
    ... code ...  # a block like this.  Just adds extra
    ... code ...  # work to maintain.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020