• Login
Community
  • Login

Tabs in python

Scheduled Pinned Locked Moved General Discussion
13 Posts 6 Posters 2.2k 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.
  • F
    Frezy Frezy
    last edited by Aug 1, 2019, 5:38 AM

    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?

    E 2 Replies Last reply Aug 1, 2019, 8:49 AM Reply Quote 0
    • E
      Ekopalypse @Frezy Frezy
      last edited by Aug 1, 2019, 8:49 AM

      @Frezy-Frezy

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

      1 Reply Last reply Reply Quote 4
      • P
        PeterJones
        last edited by Aug 1, 2019, 1:17 PM

        @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
        • A
          Alan Kilborn
          last edited by Alan Kilborn Aug 1, 2019, 1:27 PM Aug 1, 2019, 1:26 PM

          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
          • E
            Ekopalypse @Frezy Frezy
            last edited by Aug 2, 2019, 12:02 PM

            @Frezy-Frezy

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

            A 1 Reply Last reply Aug 2, 2019, 12:28 PM Reply Quote 2
            • A
              Alan Kilborn @Ekopalypse
              last edited by Aug 2, 2019, 12:28 PM

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

              E 1 Reply Last reply Aug 2, 2019, 12:46 PM Reply Quote 2
              • E
                Ekopalypse @Alan Kilborn
                last edited by Aug 2, 2019, 12:46 PM

                @Alan-Kilborn

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

                A 1 Reply Last reply Aug 2, 2019, 12:49 PM Reply Quote 2
                • A
                  Alan Kilborn @Ekopalypse
                  last edited by Aug 2, 2019, 12:49 PM

                  @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
                  • A
                    ajay gohil
                    last edited by Aug 5, 2019, 4:16 AM

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

                    E 1 Reply Last reply Aug 5, 2019, 10:29 AM Reply Quote 0
                    • E
                      Ekopalypse @ajay gohil
                      last edited by Aug 5, 2019, 10:29 AM

                      @ajay-gohil

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

                      A 1 Reply Last reply Aug 5, 2019, 12:14 PM Reply Quote 0
                      • A
                        Alan Kilborn @Ekopalypse
                        last edited by Aug 5, 2019, 12:14 PM

                        @Ekopalypse

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

                        E 1 Reply Last reply Aug 5, 2019, 12:34 PM Reply Quote 2
                        • E
                          Ekopalypse @Alan Kilborn
                          last edited by Aug 5, 2019, 12:34 PM

                          @Alan-Kilborn

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

                          @ajay-gohil - no offense.

                          1 Reply Last reply Reply Quote 2
                          • M
                            Michael Mannion
                            last edited by Aug 16, 2019, 8:43 PM

                            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