Python Feature Request: Explicit variable declarations

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

    #16
    Re: Python Feature Request: Explicit variable declarations

    On Apr 14, 3:56 pm, "Martin v. Löwis" <mar...@v.loewi s.dewrote:
    for prior discussion. The request is sane, but is also incomplete: there
    is no syntax suggested for the actual declarations of local variables,
    and no discussion whether this is meant to apply to local variables
    only, or also to global variables and object attributes.
    I suggest that the C syntax be followed:

    int a, b, c
    a = "foo" should raise a TypeError

    But as has been pointed out in the intervening messages which are
    slightly OT, the line int a, b, c should mean:

    "a, b and c are meant to be labels of integer objects so don't allow
    them to be labels of other kinds of objects without redeclaration",

    and NOT the following:

    "create three memory spaces for integers with labels a, b and c"

    as it would be in C. This ensures that Python's behaviour of:
    >>a = 2
    >>b = a
    >>a is b
    True

    continues even after int a, b. If the C meaning were taken, a is b
    would never be True.

    I do not see why this cannot be applicable to global variables also.
    When explicit type declarations is enabled, no label can be used
    implicitly for an object unless it has been declared that that label
    is meant for that kind of object. If that label is reassigned to an
    object of a different type, a TypeError should be raised. If a label
    that has not been declared is used for an object, a
    VariableNotDecl aredError should be raised.

    This can be used for object attributes also. When a programmer creates
    a class, he would be expected to type-declare the members.

    This also means that function arguments and return values should be
    type-declared:

    def bool printNameAndAge ( str name, int age ) :
    _print name
    _print age
    _return True

    This option should be set for each source file. Otherwise it will
    raise errors in modules which were written without type declaration
    (and maybe in modules written in C too ??).

    I realize this may be a big thing to include in a language which has
    been traditionally dynamically-typed, but it can ease transitions to
    lower-level languages like C, especially since one of the most
    important uses of Python is learning programming. It is also useful to
    pure Python programmers as said above for error-catching.

    Comment

    • Ganesan Rajagopal

      #17
      Re: Python Feature Request: Explicit variable declarations

      >>>>"Martin" == Martin v Löwis <martin@v.loewi s.dewrites:
      samjnaa@gmail.c om schrieb:
      >Hello. Please tell me whether this feature request is sane (and not
      >done before) for python so it can be posted to the python-dev mailing
      >list. I should say first that I am not a professional programmer with
      >too much technical knowledge.
      I believe it has been requested before, but I'm too lazy now to search
      for prior discussion.
      I remember the benevolent dictator's blogs on this. Googling for "optional
      static typing" brought up these references:




      and an older one:


      The request is sane, but is also incomplete: there
      is no syntax suggested for the actual declarations of local variables,
      and no discussion whether this is meant to apply to local variables
      only, or also to global variables and object attributes.
      None of the above links talk about variable declarations but object
      attributes are considered.

      Ganesan

      --
      Ganesan Rajagopal

      Comment

      • Steve Holden

        #18
        Re: That might be the case for more complex objects...

        James Stroud wrote:
        Bart Willems wrote:
        >Dennis Lee Bieber wrote:
        [...]
        >Lists behave as described above, integers and floats don't.
        >>
        >By the way, a classic language like C has features like this too;
        >they're called pointers.
        >
        I think that after a += 1, a memory location with a 6 is created and now
        a points to that because += has assignment buried in it.
        This is the difference between mutable and immutable types.
        >>a = 5
        >>b = a
        >>a += 1
        >>a
        6
        >>b
        5
        >>a = [1,2,3,4,5]
        >>b = a
        >>a += [6,7,8]
        >>a
        [1, 2, 3, 4, 5, 6, 7, 8]
        >>b
        [1, 2, 3, 4, 5, 6, 7, 8]
        >>>
        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC/Ltd http://www.holdenweb.com
        Skype: holdenweb http://del.icio.us/steve.holden
        Recent Ramblings http://holdenweb.blogspot.com

        Comment

        • Jorgen Grahn

          #19
          Re: Python Feature Request: Explicit variable declarations

          On 14 Apr 2007 03:21:18 -0700, samjnaa@gmail.c om <samjnaa@gmail. comwrote:
          ....
          I would like to have something like the "option explicit" statement in
          Visual Basic which turns on C-like checking for declaration of
          variables. This is highly helpful for people who are coming from C/C+
          +, for people who are learning programming using Python,
          As a C and C++ programmer (not a C/C++ programmer), I have to say that
          it doesn't bother me much. An optional feature like this would just
          annoy me if I encountered code using it.

          ....
          where the user has inadvertantly typed "g" before "n" and will later
          wonder why his application is not working as expected.
          Well, he won't *wonder* -- Python will be rather explicit about it.
          "Later" is the key word here -- this bug may lay around waiting for
          ten years before someone triggers it.

          /Jorgen

          --
          // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
          \X/ snipabacken.dyn dns.org R'lyeh wgah'nagl fhtagn!

          Comment

          • Kumar McMillan

            #20
            Re: Python Feature Request: Explicit variable declarations

            On 4/14/07, James Stroud <jstroud@mbi.uc la.eduwrote:
            samjnaa@gmail.c om wrote:
            But, if you have masochistic tendencies and want a lot of overhead in
            your programming, you can always bind yourself mercilessly to classes:
            >
            >
            class C(object):
            declared = ['bob', 'carol', 'ted', 'alice']
            def __setattr__(sel f, anattr, aval):
            if anattr not in C.declared:
            raise TypeError, "Just can't hook you up, bro."
            else:
            self.__dict__[anattr] = aval
            >
            You could also do this with __slots__, like:
            >>class C(object):
            .... __slots__ = ['bob','carol',' ted','alice']
            ....
            >>c = C()
            >>c.bob = 42
            >>c.bob
            42
            >>c.x = 69
            Traceback (most recent call last):
            File "<stdin>", line 1, in ?
            AttributeError: 'C' object has no attribute 'x'


            although, I personally have never found the need nor desire to use __slots__


            >
            E.g.:
            >
            pyclass C(object):
            ... declared = ['bob', 'carol', 'ted', 'alice']
            ... def __setattr__(sel f, anattr, aval):
            ... if anattr not in C.declared:
            ... raise TypeError, "Just can't hook you up, bro."
            ... else:
            ... self.__dict__[anattr] = aval
            ...
            pyc = C()
            pyc.bob = 42
            pyc.bob
            42
            pyc.x = 69
            Traceback (most recent call last):
            File "<stdin>", line 1, in <module>
            File "<stdin>", line 5, in __setattr__
            TypeError: Just can't hook you up, bro.
            >
            >
            Extending this ugliness to type-checking is left as an exercise for the
            reader. Using metaclasses for such nonsense is left as an exercise for
            metaclass maniacs.
            >
            James
            --

            >

            Comment

            • Alex Martelli

              #21
              Re: Python Feature Request: Explicit variable declarations

              Jorgen Grahn <grahn+nntp@sni pabacken.dyndns .orgwrote:
              As a C and C++ programmer (not a C/C++ programmer), I have to say that
              Yeah, I wonder, what's C divided by C++ -- maybe about 0.731...?


              Alex

              Comment

              • Marc 'BlackJack' Rintsch

                #22
                Re: Python Feature Request: Explicit variable declarations

                In <1hwu43c.dp5yo0 1rsqhjfN%aleax@ mac.com>, Alex Martelli wrote:
                Jorgen Grahn <grahn+nntp@sni pabacken.dyndns .orgwrote:
                >
                >As a C and C++ programmer (not a C/C++ programmer), I have to say that
                >
                Yeah, I wonder, what's C divided by C++ -- maybe about 0.731...?
                Isn't it 1 unless `C` is 0? The increment happens after the division,
                right? :-)

                Ciao,
                Marc 'BlackJack' Rintsch

                Comment

                • Gabriel Genellina

                  #23
                  Re: Python Feature Request: Explicit variable declarations

                  En Fri, 20 Apr 2007 00:41:04 -0300, Alex Martelli <aleax@mac.come scribió:
                  Jorgen Grahn <grahn+nntp@sni pabacken.dyndns .orgwrote:
                  >
                  >As a C and C++ programmer (not a C/C++ programmer), I have to say that
                  >
                  Yeah, I wonder, what's C divided by C++ -- maybe about 0.731...?
                  C/C++ == 1 most of the time (being C of any integer type, of course - for
                  arbitrary instances, see the always missing documentation).

                  --
                  Gabriel Genellina

                  Comment

                  • Beliavsky

                    #24
                    Re: Python Feature Request: Explicit variable declarations

                    On Apr 14, 6:21 am, samj...@gmail.c om wrote:
                    Hello. Please tell me whether this feature request is sane (and not
                    done before) for python so it can be posted to the python-dev mailing
                    list. I should say first that I am not a professional programmer with
                    too much technical knowledge.
                    >
                    I would like to have something like the "option explicit" statement in
                    Visual Basic which turns on C-like checking for declaration of
                    variables.
                    The thoughts of the inventor of Python on "Adding Optional Static
                    Typing to Python" are at http://www.artima.com/weblogs/viewpost.jsp?thread=86641
                    .. I wonder if the idea will be implemented in Python 3.0.

                    Comment

                    • Bruno Desthuilliers

                      #25
                      Re: Python Feature Request: Explicit variable declarations

                      samjnaa@gmail.c om a écrit :
                      Hello. Please tell me whether this feature request is sane (and not
                      done before) for python so it can be posted to the python-dev mailing
                      list. I should say first that I am not a professional programmer with
                      too much technical knowledge.
                      >
                      I would like to have something like the "option explicit" statement in
                      Visual Basic which turns on C-like checking for declaration of
                      variables. This is highly helpful for people who are coming from C/C+
                      +, for people who are learning programming using Python, and even
                      professionals, since this helps prevent typo errors like:
                      >
                      sunlognitude = sunlongitude + 180.0
                      >
                      where the user has inadvertantly typed "g" before "n" and will later
                      wonder why his application is not working as expected.
                      >
                      if you find declarative static typing programmer-friendly, then Python
                      is definitively not for you, and you'd better look for something else
                      (Java ?). FWIW, the first and main reason for stating typing is about
                      compiler's hint and raw performances, not "type-safety".

                      (A professional programmer with restricted technical knowledge and
                      absolutely no need for this kind of "help")

                      Comment

                      • Eduardo \EdCrypt\ O. Padoan

                        #26
                        Re: Python Feature Request: Explicit variable declarations

                        The thoughts of the inventor of Python on "Adding Optional Static
                        Typing to Python" are at http://www.artima.com/weblogs/viewpost.jsp?thread=86641
                        . I wonder if the idea will be implemented in Python 3.0.
                        No. He says it in another newer post and in PEP 3099, AFAIK.


                        --
                        EduardoOPadoan (eopadoan->altavix::com )
                        Bookmarks: http://del.icio.us/edcrypt

                        Comment

                        • Bjoern Schliessmann

                          #27
                          Re: Python Feature Request: Explicit variable declarations

                          Dennis Lee Bieber wrote:
                          Unless it should be interpreted as (C/C)++, which would result in
                          2
                          No, since postfix ++ "returns before and increments after". But what
                          you say is true for ++(C/C).

                          Regards,


                          Björn

                          --
                          BOFH excuse #274:

                          It was OK before you touched it.

                          Comment

                          Working...