python newbie: some surprises

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • v4vijayakumar

    python newbie: some surprises

    When I started coding in python, these two things surprised me.

    1. my code is inconsistently indented with the combination of tabs and
    spaces. Even lines looked intended, but it is not.

    2. python requires to pass "self" to all instance methods

    and I missed ":" often. :)
  • Martin P. Hellwig

    #2
    Re: python newbie: some surprises

    v4vijayakumar wrote:
    When I started coding in python, these two things surprised me.
    >
    1. my code is inconsistently indented with the combination of tabs and
    spaces. Even lines looked intended, but it is not.
    >
    Even the standard editor Idle tries to guess the intendation, so this
    was never a problem for me. Though these days I use PyDev a lot.
    2. python requires to pass "self" to all instance methods
    A lot of editors help you with these 'unnecessary' things by
    auto-completion might be worth looking into one.
    >
    and I missed ":" often. :)
    Still do after 5 years of python abuse :-)

    --
    mph

    Comment

    • Mensanator

      #3
      Re: python newbie: some surprises

      On May 8, 2:06 am, v4vijayakumar <vijayakumar.su bbu...@gmail.co m>
      wrote:
      When I started coding in python, these two things surprised me.
      >
      1. my code is inconsistently indented with the combination of tabs and
      spaces. Even lines looked intended, but it is not.
      You must type inconsistently.

      I never had such a problem even when I used to use Notepad.
      >
      2. python requires to pass "self" to all instance methods
      Who uses methods?
      >
      and I missed ":" often. :)
      Try using something like Seed7, where you have to use "then" with
      "if" and "do" with "while" and "end" in every block. Maybe you'll
      come to appreciate significant whitespace and ":".

      Comment

      • Yves Dorfsman

        #4
        Re: python newbie: some surprises

        Mensanator wrote:
        >2. python requires to pass "self" to all instance methods
        >
        Who uses methods?
        Is this a joke ?
        What are the alternatives ?

        >
        >and I missed ":" often. :)
        >
        Try using something like Seed7, where you have to use "then" with
        "if" and "do" with "while" and "end" in every block. Maybe you'll
        come to appreciate significant whitespace and ":".
        I see the point of the OP. Couldn't the new-line be used as an equivalent of
        ':', for example, do you find this difficult to read:

        if a == 3
        do_something()


        if a == 3: do_something()


        And surely, it should be easy to parse by the compiler.

        Yves.
        Calgary AIX Linux UNIX React TypeScript JavaScript python contractor consultant programmer Yves Dorfsman

        Comment

        • Mensanator

          #5
          Re: python newbie: some surprises

          On May 8, 11:47�pm, Yves Dorfsman <y...@zioup.com wrote:
          Mensanator wrote:
          2. python requires to pass "self" to all instance methods
          >
          Who uses methods?
          >
          Is this a joke ?
          Yes.
          What are the alternatives ?
          >
          >
          >
          and I missed ":" often. :)
          >
          Try using something like Seed7, where you have to use "then" with
          "if" and "do" with "while" and "end" in every block. Maybe you'll
          come to appreciate significant whitespace and ":".
          >
          I see the point of the OP. Couldn't the new-line be used as an equivalent of
          � ':', for example, do you find this difficult to read:
          >
          if a == 3
          � �do_something ()
          >
          if a == 3: do_something()
          >
          And surely, it should be easy to parse by the compiler.
          If they were to chane it, I wouldn't complain.

          I just think it doesn't deserve complaints when
          compared to other systems.

          Comment

          • Gabriel Genellina

            #6
            Re: python newbie: some surprises

            En Fri, 09 May 2008 01:47:49 -0300, Yves Dorfsman <yves@zioup.com >
            escribió:
            I see the point of the OP. Couldn't the new-line be used as an
            equivalent of ':', for example, do you find this difficult to read:
            >
            if a == 3
            do_something()
            >
            >
            if a == 3: do_something()
            >
            >
            And surely, it should be easy to parse by the compiler.
            Yes, it could be done, there are no technical reasons to always force to
            use ":". But AFAIK the main reasons to keep ":" are internal consistency
            (an inner block always starts with ":"; incidentally, that's easier to
            handle for editors) and legibility (the ":" stands for itself and has a
            meaning)

            --
            Gabriel Genellina

            Comment

            • Bruno Desthuilliers

              #7
              Re: python newbie: some surprises

              v4vijayakumar a écrit :
              When I started coding in python, these two things surprised me.
              >
              1. my code is inconsistently indented with the combination of tabs and
              spaces. Even lines looked intended, but it is not.
              Then you have a problem with your code editor - not with Python.
              2. python requires to pass "self" to all instance methods
              Nope. Python requires that function used as instance methods take the
              instance as first argument (and that functions used as classmethods take
              the class as first argument). It's the method object's duty to actually
              pass the appropriate object to the function. The rational is that it
              allows to built methods above two more generic constructs (namely:
              functions and the descriptor protocol) instead of having to special-case
              them.
              and I missed ":" often. :)
              Your editor should not indent the next line then. Either you failed to
              correctly configure your editor, or it's broken.

              Comment

              • Bruno Desthuilliers

                #8
                Re: python newbie: some surprises

                Yves Dorfsman a écrit :
                Mensanator wrote:
                >>2. python requires to pass "self" to all instance methods
                >>
                >Who uses methods?
                >
                Is this a joke ?
                Very probably.
                What are the alternatives ?
                Err... functions ?-)

                Comment

                • Bruno Desthuilliers

                  #9
                  Re: python newbie: some surprises

                  Yves Dorfsman a écrit :
                  (snip)
                  I see the point of the OP. Couldn't the new-line be used as an
                  equivalent of ':',

                  Technically, yes. OTHO, the ':' helps editors doing proper indentation.

                  Comment

                  • v4vijayakumar

                    #10
                    Re: python newbie: some surprises

                    On May 9, 1:48 pm, Bruno Desthuilliers <bruno.
                    42.desthuilli.. .@websiteburo.i nvalidwrote:
                    v4vijayakumar a écrit :
                    >
                    When I started coding in python, these two things surprised me.
                    >
                    1. my code is inconsistently indented with the combination of tabs and
                    spaces. Even lines looked intended, but it is not.
                    >
                    Then you have a problem with your code editor - not with Python.
                    >
                    Editors can not be wrong. :)

                    I think there should be some way to say python compiler, to consider
                    tab and two blank spaces equal, when tab space = 2.

                    Comment

                    • Yves Dorfsman

                      #11
                      Re: python newbie: some surprises

                      Gabriel Genellina wrote:
                      >I see the point of the OP. Couldn't the new-line be used as an
                      >equivalent of ':', for example, do you find this difficult to read:
                      >>
                      >if a == 3
                      > do_something()
                      >>
                      >>
                      >if a == 3: do_something()
                      >
                      Yes, it could be done, there are no technical reasons to always force to
                      use ":". But AFAIK the main reasons to keep ":" are internal consistency
                      (an inner block always starts with ":"; incidentally, that's easier to
                      handle for editors) and legibility (the ":" stands for itself and has a
                      meaning)
                      Legibility ?
                      But one could make the same argument for curly brackets, and we seem to be
                      doing fine without them !

                      I have become so used to the power of indenting in python that I keep
                      forgetting the colon, and this is getting worse as I do more python, not
                      better. Maybe I'll write myself a "pre-compiler" that add the colons where
                      the compiler needs them :-)


                      Yves.
                      Calgary AIX Linux UNIX React TypeScript JavaScript python contractor consultant programmer Yves Dorfsman


                      Comment

                      • J. Cliff Dyer

                        #12
                        Re: python newbie: some surprises

                        On Fri, 2008-05-09 at 15:08 +0000, Yves Dorfsman wrote:
                        Gabriel Genellina wrote:
                        >
                        I see the point of the OP. Couldn't the new-line be used as an
                        equivalent of ':', for example, do you find this difficult to read:
                        >
                        if a == 3
                        do_something()
                        >
                        >
                        if a == 3: do_something()
                        Yes, it could be done, there are no technical reasons to always force to
                        use ":". But AFAIK the main reasons to keep ":" are internal consistency
                        (an inner block always starts with ":"; incidentally, that's easier to
                        handle for editors) and legibility (the ":" stands for itself and has a
                        meaning)
                        >
                        Legibility ?
                        But one could make the same argument for curly brackets, and we seem to be
                        doing fine without them !
                        >
                        I have become so used to the power of indenting in python that I keep
                        forgetting the colon, and this is getting worse as I do more python, not
                        better. Maybe I'll write myself a "pre-compiler" that add the colons where
                        the compiler needs them :-)
                        >
                        >
                        Yves.
                        Calgary AIX Linux UNIX React TypeScript JavaScript python contractor consultant programmer Yves Dorfsman

                        >
                        --

                        >
                        Have you considered the following:

                        if (x == 4 and (y in
                        [len(x) for x in
                        foo if x**2 23]
                        or y < 2) and z.strip().endsw ith('z') and
                        remove_first(w) )
                        attach_list(q, r)
                        reject(x)

                        A colon on the correct line would help readability quite a bit.

                        Yeah, I know I made it pretty ugly to begin with, and there are ways to
                        improve it without the colon, but still, just because it could be
                        removed doesn't necessarily mean it should.

                        Cheers,
                        Cliff

                        Comment

                        • Gabriel Genellina

                          #13
                          Re: python newbie: some surprises

                          En Fri, 09 May 2008 10:37:30 -0300, v4vijayakumar <vijayakumar.su bburaj@gmail.co mescribió:
                          On May 9, 1:48 pm, Bruno Desthuilliers <bruno.
                          42.desthuilli.. .@websiteburo.i nvalidwrote:
                          >v4vijayakuma r a écrit :
                          >>
                          When I started coding in python, these two things surprised me.
                          >>
                          1. my code is inconsistently indented with the combination of tabs and
                          spaces. Even lines looked intended, but it is not.
                          >>
                          >Then you have a problem with your code editor - not with Python.
                          >>
                          >
                          Editors can not be wrong. :)
                          >
                          I think there should be some way to say python compiler, to consider
                          tab and two blank spaces equal, when tab space = 2.
                          It already considers tab = 8 spaces, and when invoked with -tt it rejects mixed tabs+spaces. (I would like Python rejected *any* tab used for indenting...)
                          There is a tool 'reindent.py' -somewhere on your Python install-, and an indentation checker 'tabnanny.py' (this one in the standard library).

                          --
                          Gabriel Genellina

                          Comment

                          • Kees Bakker

                            #14
                            Re: python newbie: some surprises

                            Gabriel Genellina wrote:
                            En Fri, 09 May 2008 10:37:30 -0300, v4vijayakumar <vijayakumar.su bburaj@gmail.co mescribió:
                            >
                            >On May 9, 1:48 pm, Bruno Desthuilliers <bruno.
                            >42.desthuilli. ..@websiteburo. invalidwrote:
                            >>v4vijayakum ar a écrit :
                            >>>
                            >When I started coding in python, these two things surprised me.
                            >>>
                            >1. my code is inconsistently indented with the combination of tabs and
                            >spaces. Even lines looked intended, but it is not.
                            >>>
                            >>Then you have a problem with your code editor - not with Python.
                            >>>
                            >>
                            >Editors can not be wrong. :)
                            >>
                            >I think there should be some way to say python compiler, to consider
                            >tab and two blank spaces equal, when tab space = 2.
                            >
                            It already considers tab = 8 spaces, and when invoked with -tt it rejects mixed tabs+spaces. (I would like Python rejected *any* tab used for indenting...)
                            There is a tool 'reindent.py' -somewhere on your Python install-, and an indentation checker 'tabnanny.py' (this one in the standard library).
                            >
                            That's one of the reasons why I like Python :-)

                            Still too many people don't know that you must set a TAB to 8 in
                            your editor. Anything other than 8 for a TAB will, at some point,
                            confuse somebody.

                            Don't confuse indentation with TAB setting.

                            Many editors are not helpfull either. Pydev, for example, has a setting
                            for TAB, but it is used for indentation. It is just luck (I think) that
                            pydev has an option to say that you only want spaces. (Take a look at
                            the main preferences of Pydev.)

                            So far, I have seen only one editor that understands the difference between
                            TABs and indentation, and that is Emacs.
                            --
                            Kees

                            Comment

                            • Marco Mariani

                              #15
                              Re: python newbie: some surprises

                              Kees Bakker wrote:

                              So far, I have seen only one editor that understands the difference between
                              TABs and indentation, and that is Emacs.
                              Oh, well... in .vimrc:

                              autocmd FileType python set tabstop=8
                              autocmd FileType python set softtabstop=4
                              autocmd FileType python set expandtab

                              Comment

                              Working...