Tabs in python



  • Hello, I have some problems with python files, when I try to use tabs it will make normal spaces the same size as tabs. The problem is that is ruining the code and I cannot run it. Any solution?



  • @Frezy-Frezy

    goto Settings->Preferences->Language select python from available items and
    uncheck replace by space



  • @Ekopalypse is right, as far as choosing whether the TAB button inserts the tab character or a number of spaces. However:

    @Frezy-Frezy said:

    The problem is that is ruining the code and I cannot run it. Any solution?

    Being inconsistent is what’s ruining your code. Python works just fine if all the indents are spaces. Python works just fine if all the indents are tabs. The Python references I’ve seen actually recommend spaces, not tabs. If you’d like to leave the tab-to-space settings as-is, you can instead use Edit > Blank Operations > TAB to space to convert your existing codebase to use only spaces, instead of a mixture.



  • Perhaps enabling View menu > Show Symbol > Show White Space and TAB will help @Frezy-Frezy until this is sorted out…whether by the use of the conversion Peter suggests or otherwise.



  • @Frezy-Frezy

    @PeterJones is right, as of pep8 it is recommended to use spaces instead of tabs.



  • @Ekopalypse

    I thought it was recommended that FOUR spaces = one indent level as well, but maybe my memory is faulty on that as I don’t see it in the pep8 link.

    Regardless, it is nice that N++ gives one the choice of whatever they want in regards to this. Obviously best to not mix them, however.



  • @Alan-Kilborn

    yep, 4 spaces is recommended :-)
    I know is must be quite early over there in the states ;-D



  • @Ekopalypse said:

    I know is must be quite early over there in the states

    Click original pep link, then SCROLL UP Alan!!



  • The \t inside the string is the escape sequence for the horizontal tabulation.
    f = open(filename, ‘w’)
    f.write(“hello\talex”)



  • @ajay-gohil

    Don’t get your point, what exactly are you referring to?



  • @Ekopalypse

    Suspect @ajay-gohil is in the kitchen, baking.



  • @Alan-Kilborn

    :-D - maybe, hopefully using cookiecutter :-D

    @ajay-gohil - no offense.



  • Another victim of PEP 8.

    Frenzy Frenzy, I agree with Peter Jones that inconsistency is the issue here. That said, I presume that you were being consistent and that npp silently changed your tabs to spaces, per its default behavior (yea for npp for following the rules; boo for the PEP 8 authors for making that rule).

    It shouldn’t have been an issue if you were starting a module from scratch, but would definitely lead to problems if you were editing existing code that already contained real tabs. Once the code is hosed, you have a few options in npp for cleaning it up:

    • View the white space characters, per Alan Kilborn’s post, and manually fix them

    • Use the TAB to space option Peter cites - or, ideally, it’s inverse operations, Space to TAB (All) and Space to TAB (Leading)

    • Use Search > Replace with the Search Mode > Extended option to manually find or replace tab characters using their special \t representation

    The automatic space-to-tab converters are good, but bulk operations inevitably (and understandably) overgeneralize. While time consuming, the third option gives you more control over the process, with some level of automation.

    FWIW, I’m +1 with you for using tabs. I’m always amused (or perhaps bemused) that right at the top of PEP 8 is a section called “A Foolish Consistency is the Hobgoblin of Little Minds”, followed immediately by the first, and most foolish, of its recommendations - to use spaces instead of tabs (which many in the Python community follow consistently, admittedly with good intentions).

    I’ll refrain from commenting on the merits of tabs; there’s plenty on the web about that already. Suffice it to say, there’s a large community of Python programmers that agree with your approach.


Log in to reply