Is there a way around coding in Python without the tab, indent & whitespace criteria?

I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off?

I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?


Asked by: David175 | Posted: 24-09-2021






Answer 1

The answer is no.

At least, not until something like the following is implemented:

from __future__ import braces

Answered by: Roman119 | Posted: 25-10-2021



Answer 2

No. Indentation-as-grammar is an integral part of the Python language, for better and worse.

Answered by: Kellan713 | Posted: 25-10-2021



Answer 3

Emacs! Seriously, its use of "tab is a command, not a character", is absolutely perfect for python development.

Answered by: Richard242 | Posted: 25-10-2021



Answer 4

All of the whitespace issues I had when I was starting Python were the result mixing tabs and spaces. Once I configured everything to just use one or the other, I stopped having problems.

In my case I configured UltraEdit & vim to use spaces in place of tabs.

Answered by: Madaline107 | Posted: 25-10-2021



Answer 5

It's possible to write a pre-processor which takes randomly-indented code with pseudo-python keywords like "endif" and "endwhile" and properly indents things. I had to do this when using python as an "ASP-like" language, because the whole notion of "indentation" gets a bit fuzzy in such an environment.

Of course, even with such a thing you really ought to indent sanely, at which point the conveter becomes superfluous.

Answered by: Patrick509 | Posted: 25-10-2021



Answer 6

I find it hard to understand when people flag this as a problem with Python. I took to it immediately and actually find it's one of my favourite 'features' of the language :)

In other languages I have two jobs: 1. Fix the braces so the computer can parse my code 2. Fix the indentation so I can parse my code.

So in Python I have half as much to worry about ;-)

(nb the only time I ever have problem with indendation is when Python code is in a blog and a forum that messes with the white-space but this is happening less and less as the apps get smarter)

Answered by: Haris194 | Posted: 25-10-2021



Answer 7

I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?

I liked pydev extensions of eclipse for that.

Answered by: Daisy656 | Posted: 25-10-2021



Answer 8

I do not believe so, as Python is a whitespace-delimited language. Perhaps a text editor or IDE with auto-indentation would be of help. What are you currently using?

Answered by: Lydia922 | Posted: 25-10-2021



Answer 9

No, there isn't. Indentation is syntax for Python. You can:

  1. Use tabnanny.py to check your code
  2. Use a syntax-aware editor that highlights such mistakes (vi does that, emacs I bet it does, and then, most IDEs do too)
  3. (far-fetched) write a preprocessor of your own to convert braces (or whatever block delimiters you love) into indentation

Answered by: Sawyer903 | Posted: 25-10-2021



Answer 10

You should disable tab characters in your editor when you're working with Python (always, actually, IMHO, but especially when you're working with Python). Look for an option like "Use spaces for tabs": any decent editor should have one.

Answered by: David183 | Posted: 25-10-2021



Answer 11

I agree with justin and others -- pick a good editor and use spaces rather than tabs for indentation and the whitespace thing becomes a non-issue. I only recently started using Python, and while I thought the whitespace issue would be a real annoyance it turns out to not be the case. For the record I'm using emacs though I'm sure there are other editors out there that do an equally fine job.

If you're really dead-set against it, you can always pass your scripts through a pre-processor but that's a bad idea on many levels. If you're going to learn a language, embrace the features of that language rather than try to work around them. Otherwise, what's the point of learning a new language?

Answered by: Kelvin784 | Posted: 25-10-2021



Answer 12

Not really. There are a few ways to modify whitespace rules for a given line of code, but you will still need indent levels to determine scope.

You can terminate statements with ; and then begin a new statement on the same line. (Which people often do when golfing.)

If you want to break up a single line into multiple lines you can finish a line with the \ character which means the current line effectively continues from the first non-whitespace character of the next line. This visually appears violate the usual whitespace rules but is legal.

My advice: don't use tabs if you are having tab/space confusion. Use spaces, and choose either 2 or 3 spaces as your indent level.

A good editor will make it so you don't have to worry about this. (python-mode for emacs, for example, you can just use the tab key and it will keep you honest).

Answered by: Clark619 | Posted: 25-10-2021



Answer 13

Tabs and spaces confusion can be fixed by setting your editor to use spaces instead of tabs.

To make whitespace completely intuitive, you can use a stronger code editor or an IDE (though you don't need a full-blown IDE if all you need is proper automatic code indenting).

A list of editors can be found in the Python wiki, though that one is a bit too exhausting: - http://wiki.python.org/moin/PythonEditors

There's already a question in here which tries to slim that down a bit:

Maybe you should add a more specific question on that: "Which Python editor or IDE do you prefer on Windows - and why?"

Answered by: Hailey449 | Posted: 25-10-2021



Answer 14

Getting your indentation to work correctly is going to be important in any language you use.

Even though it won't affect the execution of the program in most other languages, incorrect indentation can be very confusing for anyone trying to read your program, so you need to invest the time in figuring out how to configure your editor to align things correctly.

Python is pretty liberal in how it lets you indent. You can pick between tabs and spaces (but you really should use spaces) and can pick how many spaces. The only thing it requires is that you are consistent which ultimately is important no matter what language you use.

Answered by: Elian548 | Posted: 25-10-2021



Answer 15

I was a bit reluctant to learn Python because of tabbing. However, I almost didn't notice it when I used Vim.

Answered by: Miller685 | Posted: 25-10-2021



Answer 16

If you don't want to use an IDE/text editor with automatic indenting, you can use the pindent.py script that comes in the Tools\Scripts directory. It's a preprocessor that can convert code like:

def foobar(a, b):
if a == b:
a = a+1
elif a < b:
b = b-1
if b > a: a = a-1
end if
else:
print 'oops!'
end if
end def foobar

into:

def foobar(a, b):
   if a == b:
       a = a+1
   elif a < b:
       b = b-1
       if b > a: a = a-1
       # end if
   else:
       print 'oops!'
   # end if
# end def foobar

Which is valid python.

Answered by: Fiona900 | Posted: 25-10-2021



Answer 17

Nope, there's no way around it, and it's by design:

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance

Most Python programmers simply don't use tabs, but use spaces to indent instead, that way there's no editor-to-editor inconsistency.

Answered by: Alberta383 | Posted: 25-10-2021



Answer 18

I'm surprised no one has mentioned IDLE as a good default python editor. Nice syntax colors, handles indents, has intellisense, easy to adjust fonts, and it comes with the default download of python. Heck, I write mostly IronPython, but it's so nice & easy to edit in IDLE and run ipy from a command prompt.

Oh, and what is the big deal about whitespace? Most easy to read C or C# is well indented, too, python just enforces a really simple formatting rule.

Answered by: John609 | Posted: 25-10-2021



Answer 19

Many Python IDEs and generally-capable text/source editors can handle the whitespace for you.

However, it is best to just "let go" and enjoy the whitespace rules of Python. With some practice, they won't get into your way at all, and you will find they have many merits, the most important of which are:

  1. Because of the forced whitespace, Python code is simpler to understand. You will find that as you read code written by others, it is easier to grok than code in, say, Perl or PHP.
  2. Whitespace saves you quite a few keystrokes of control characters like { and }, which litter code written in C-like languages. Less {s and }s means, among other things, less RSI and wrist pain. This is not a matter to take lightly.

Answered by: Lily456 | Posted: 25-10-2021



Answer 20

In Python, indentation is a semantic element as well as providing visual grouping for readability.

Both space and tab can indicate indentation. This is unfortunate, because:

  • The interpretation(s) of a tab varies among editors and IDEs and is often configurable (and often configured).

  • OTOH, some editors are not configurable but apply their own rules for indentation.

  • Different sequences of spaces and tabs may be visually indistinguishable.

  • Cut and pastes can alter whitespace.

So, unless you know that a given piece of code will only be modified by yourself with a single tool and an unvarying config, you must avoid tabs for indentation (configure your IDE) and make sure that you are warned if they are introduced (search for tabs in leading whitespace).

And you can still expect to be bitten now and then, as long as arbitrary semantics are applied to control characters.

Answered by: Walter641 | Posted: 25-10-2021



Answer 21

Check the options of your editor or find an editor/IDE that allows you to convert TABs to spaces. I usually set the options of my editor to substitute the TAB character with 4 spaces, and I never run into any problems.

Answered by: Alfred821 | Posted: 25-10-2021



Answer 22

Yes, there is a way. I hate these "no way" answers, there is no way until you discover one.

And in that case, whatever it is worth, there is one.

I read once about a guy who designed a way to code so that a simple script could re-indent the code properly. I didn't managed to find any links today, though, but I swear I read it.

The main tricks are to always use return at the end of a function, always use pass at the end of an if or at the end of a class definition, and always use continue at the end of a while. Of course, any other no-effect instruction would fit the purpose.

Then, a simple awk script can take your code and detect the end of block by reading pass/continue/return instructions, and the start of code with if/def/while/... instructions.

Of course, because you'll develop your indenting script, you'll see that you don't have to use continue after a return inside the if, because the return will trigger the indent-back mechanism. The same applies for other situations. Just get use to it.

If you are diligent, you'll be able to cut/paste and add/remove if and correct the indentations automagically. And incidentally, pasting code from the web will require you to understand a bit of it so that you can adapt it to that "non-classical" setting.

Answered by: Roland684 | Posted: 25-10-2021



Similar questions

Significant whitespace in C# like Python or Haskell?


Strip whitespace in generated HTML using pure Python code

I am using Jinja2 to generate HTML files which are typically very huge in size. I noticed that the generated HTML had a lot of whitespace. Is there a pure-Python tool that I can use to minimize this HTML? When I say "minimize", I mean remove unnecessary whitespace from the HTML (much like Google does -- look at the source for google.com, for instance) I don't want to rely on libraries/external-executables such as t...


python - Regex for removing whitespace

def remove_whitespaces(value): "Remove all whitespaces" p = re.compile(r'\s+') return p.sub(' ', value) The above code strips tags but doesn't remove "all" whitespaces from the value. Thanks


Returning the lowest index for the first non whitespace character in a string in Python

What's the shortest way to do this in Python? string = " xyz" must return index = 3


python - Check if string contains only whitespace

How can I test if a string contains only whitespace? Example strings: &quot; &quot; (space, space, space) &quot; \t \n &quot; (space, tab, space, newline, space) &quot;\n\n\n\t\n&quot; (newline, newline, newline, tab, newline)


python - Anyone know a good regex to remove extra whitespace?

This question already has answers here:


python - Escape whitespace in paths using nautilus script

I didn't think this would be as tricky as it turned out to be, but here I am. I'm trying to write a Nautilus script in Python to upload one or more images to Imgur just by selecting and right clicking them. It works well enough with both single images and multiple images - as long as they don't contain any whitespace. In fact, you can upload a single image containing whitespace, just not multiple ones. The problem ...


string - How to do a Python split() on languages (like Chinese) that don't use whitespace as word separator?

I want to split a sentence into a list of words. For English and European languages this is easy, just use split() &gt;&gt;&gt; "This is a sentence.".split() ['This', 'is', 'a', 'sentence.'] But I also need to deal with sentences in languages such as Chinese that don't use whitespace as word separator. &gt;&gt;&gt; u"这是一个句子".split() [u'\u8fd9\u662f\u4e00\u4e2a...


python print and whitespace

I am trying to print a result for example: for record in result: print varone,vartwo,varthree I am trying to concatenate the variables which are from an SQL query, but I am getting whitespace. How can I strip whitespace from a 'print'? Should I feed the result into a variable then do a 'strip(newvar)' then print the 'newvar'?


ide - Changing whitespace color in Stani Python Editor

I figured out how to edit the looks of SPE colors. Everything works except when I try change the whitespace color: whitespace remains white. Below is a code snippet showing value input for the whitespace background. How can I change the whitespace color? # Global default styles for all languages self.StyleSetSpec(wx_stc.STC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % self.faces) ...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top