Community
    • Login

    Tabs in python

    Scheduled Pinned Locked Moved General Discussion
    13 Posts 6 Posters 2.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Frezy FrezyF
      Frezy Frezy
      last edited by

      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?

      EkopalypseE 2 Replies Last reply Reply Quote 0
      • EkopalypseE
        Ekopalypse @Frezy Frezy
        last edited by

        @Frezy-Frezy

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

        1 Reply Last reply Reply Quote 4
        • PeterJonesP
          PeterJones
          last edited by

          @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.

          1 Reply Last reply Reply Quote 3
          • Alan KilbornA
            Alan Kilborn
            last edited by Alan Kilborn

            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.

            1 Reply Last reply Reply Quote 2
            • EkopalypseE
              Ekopalypse @Frezy Frezy
              last edited by

              @Frezy-Frezy

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

              Alan KilbornA 1 Reply Last reply Reply Quote 2
              • Alan KilbornA
                Alan Kilborn @Ekopalypse
                last edited by

                @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.

                EkopalypseE 1 Reply Last reply Reply Quote 2
                • EkopalypseE
                  Ekopalypse @Alan Kilborn
                  last edited by

                  @Alan-Kilborn

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

                  Alan KilbornA 1 Reply Last reply Reply Quote 2
                  • Alan KilbornA
                    Alan Kilborn @Ekopalypse
                    last edited by

                    @Ekopalypse said:

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

                    Click original pep link, then SCROLL UP Alan!!

                    1 Reply Last reply Reply Quote 2
                    • ajay gohilA
                      ajay gohil
                      last edited by

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

                      EkopalypseE 1 Reply Last reply Reply Quote 0
                      • EkopalypseE
                        Ekopalypse @ajay gohil
                        last edited by

                        @ajay-gohil

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

                        Alan KilbornA 1 Reply Last reply Reply Quote 0
                        • Alan KilbornA
                          Alan Kilborn @Ekopalypse
                          last edited by

                          @Ekopalypse

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

                          EkopalypseE 1 Reply Last reply Reply Quote 2
                          • EkopalypseE
                            Ekopalypse @Alan Kilborn
                            last edited by

                            @Alan-Kilborn

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

                            @ajay-gohil - no offense.

                            1 Reply Last reply Reply Quote 2
                            • Michael MannionM
                              Michael Mannion
                              last edited by

                              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.

                              1 Reply Last reply Reply Quote 1
                              • First post
                                Last post
                              The Community of users of the Notepad++ text editor.
                              Powered by NodeBB | Contributors