Static Typing in Python

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

    Static Typing in Python

    How do I force static typing in Python?

    -Premshree Pillai

    =====
    -Premshree
    [http://www.qiksearch.com/]

    _______________ _______________ _______________ _______________ ____________
    Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more.
    Go to: http://in.insurance.yahoo.com/licspecial/index.html

  • John Roth

    #2
    Re: Static Typing in Python

    "Premshree Pillai" <premshree_pyth on@yahoo.co.in> wrote in message
    news:mailman.30 7.1079086151.19 534.python-list@python.org ...[color=blue]
    > How do I force static typing in Python?[/color]

    By using another language. Try Java.

    John Roth[color=blue]
    >
    > -Premshree Pillai
    >
    > =====
    > -Premshree[/color]



    Comment

    • Peter Maas

      #3
      Re: Static Typing in Python

      Premshree Pillai schrieb:[color=blue]
      > How do I force static typing in Python?[/color]

      You have to enforce it by code instead of declaration, i.e. you
      have to do runtime type checking. This could be done e.g. within
      a class:

      class doesTypeCheckin g:
      def __init__self():
      aString = ""
      aFloat = 0.0

      def __setAttr__(sel f, attr, value):
      if type(self.__dic t__[attr] != type(value):
      raise ValueError, "Type mismatch for attribute %s\n" % attr

      Similar code could be used to check parameters of a function/
      method.

      Have looked at pychecker? It's not part of the python
      distribution but it could help here.

      Mit freundlichen Gruessen,

      Peter Maas

      --
      -------------------------------------------------------------------
      Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
      Tel +49-241-93878-0 Fax +49-241-93878-20 eMail peter.maas@mplu sr.de
      -------------------------------------------------------------------

      Comment

      • Peter Maas

        #4
        Re: Static Typing in Python

        Peter Maas schrieb:[color=blue]
        > class doesTypeCheckin g:
        > def __init__self():[/color]

        Correction:
        def __init__(self):

        --
        -------------------------------------------------------------------
        Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
        Tel +49-241-93878-0 Fax +49-241-93878-20 eMail peter.maas@mplu sr.de
        -------------------------------------------------------------------

        Comment

        • Jacek Generowicz

          #5
          Re: Static Typing in Python

          Peter Maas <fpetermaas@net scape.net> writes:
          [color=blue]
          > Premshree Pillai schrieb:[color=green]
          > > How do I force static typing in Python?[/color]
          >
          > You have to enforce it by code instead of declaration, i.e. you
          > have to do runtime type checking.[/color]

          Just what do you understand "static typing" to mean ?

          Comment

          • Peter Maas

            #6
            Re: Static Typing in Python

            Peter Maas schrieb:[color=blue]
            > class doesTypeCheckin g:
            > def __init__(self):
            > aString = ""
            > aFloat = 0.0[/color]

            sorry, I was too hasty. Here is the tested code:

            class doesTypeCheckin g:
            def __init__(self):
            self.__dict__["aString"] = ""
            self.__dict__["aFloat"] = 0.0

            def __setattr__(sel f, attr, value):
            if type(self.__dic t__[attr]) != type(value):
            raise ValueError, "Type mismatch for attribute %s\n" % attr

            Mit freundlichen Gruessen,

            Peter Maas

            --
            -------------------------------------------------------------------
            Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
            Tel +49-241-93878-0 Fax +49-241-93878-20 eMail peter.maas@mplu sr.de
            -------------------------------------------------------------------

            Comment

            • Dang Griffith

              #7
              Re: Static Typing in Python

              On 12 Mar 2004 13:56:53 +0100, Jacek Generowicz
              <jacek.generowi cz@cern.ch> wrote:
              [color=blue]
              >Peter Maas <fpetermaas@net scape.net> writes:
              >[color=green]
              >> Premshree Pillai schrieb:[color=darkred]
              >> > How do I force static typing in Python?[/color]
              >>
              >> You have to enforce it by code instead of declaration, i.e. you
              >> have to do runtime type checking.[/color]
              >
              >Just what do you understand "static typing" to mean ?[/color]
              Doesn't that usually mean typing with a high noise-to-signal ratio?
              Lots of punctuation and such.
              ;-)
              --dang

              Comment

              • Jacek Generowicz

                #8
                Re: Static Typing in Python

                Jacek Generowicz <jacek.generowi cz@cern.ch> writes:
                [color=blue]
                > Peter Maas <fpetermaas@net scape.net> writes:
                >[color=green]
                > > Premshree Pillai schrieb:[color=darkred]
                > > > How do I force static typing in Python?[/color]
                > >
                > > You have to enforce it by code instead of declaration, i.e. you
                > > have to do runtime type checking.[/color]
                >
                > Just what do you understand "static typing" to mean ?[/color]

                I ask because I am prepared to accept that you have a different
                working definition of "static typing" from mine, but people who
                actually want static typing typically want it because they think it
                gives them the following advantages:

                a) type errors caught at compile time,

                b) faster program exectution.

                The code you showed:
                [color=blue][color=green]
                > > class doesTypeCheckin g:
                > > def __init__(self):
                > > self.__dict__["aString"] = ""
                > > self.__dict__["aFloat"] = 0.0
                > >
                > > def __setattr__(sel f, attr, value):
                > > if type(self.__dic t__[attr]) != type(value):
                > > raise ValueError, "Type mismatch for attribute %s\n" % attr[/color][/color]

                will catch no type errors at compile time, and will slow down
                execution, so I suspect that Premshree will be disapponinted with it.


                (BTW, my definition of "static typing" is "type checking is done at
                compile time" ... your example really looks like dynamic typing to
                me ... albeit with some restrictions on attribute types.)


                Premshree: Python is dynamically typed. There is no way to enforce
                static typing. There is something called "pychecker" which
                might be of some help to you. Why do you think that you
                want static typing in Python ?

                Comment

                • Jacek Generowicz

                  #9
                  Re: Static Typing in Python

                  Dang Griffith <noemail@noemai l4u.com> writes:
                  [color=blue]
                  > On 12 Mar 2004 13:56:53 +0100, Jacek Generowicz
                  > <jacek.generowi cz@cern.ch> wrote:[/color]
                  [color=blue][color=green]
                  > >Just what do you understand "static typing" to mean ?[/color]
                  > Doesn't that usually mean typing with a high noise-to-signal ratio?[/color]

                  What, you mean like this:

                  std::list<std:: pair<int, std::string> > l;
                  l.push_back(std ::pair<int, std::string>(1, "hello"));

                  as opposed to

                  l = [(1, 'hello')]

                  ?

                  I like your definition :-)

                  Comment

                  • Dang Griffith

                    #10
                    Re: Static Typing in Python

                    On Sat, 13 Mar 2004 07:07:50 +0000 (GMT), Premshree Pillai
                    <premshree_pyth on@yahoo.co.in> wrote:
                    [color=blue]
                    > --- Jacek Generowicz <jacek.generowi cz@cern.ch>
                    >Yes, I am aware that Python is dynamically typed, and
                    >so is Perl, right? In Perl, we have the "use strict
                    >vars" pragma to force variable declaration. Is there
                    >something like it in Python?[/color]

                    No, but you can use pychecker to get similar results.
                    [color=blue]
                    >Don't you think forced variable declaration is an
                    >important requirement in a language?[/color]

                    Not really. Forced variable initialization is what's important.
                    Unlike C, et al, and Perl, variables don't have a default
                    initial value. If you try reference a variable that hasn't been
                    initialized ("bound to a value", in python lingo), python raises a
                    NameError exception.

                    --dang
                    p.s.
                    I know technically Perl initializes to 'undef', but it's magically
                    treated as 0 or an empty string, depending on context, so the
                    effect is much the same.

                    Comment

                    • Terry Reedy

                      #11
                      Re: Static Typing in Python


                      "Premshree Pillai" <premshree_pyth on@yahoo.co.in> wrote in message[color=blue]
                      > Anyway, what I want is to force variable declaration.
                      > In perl we have the "use strict vars" pargma...I need
                      > something like that...[/color]

                      For those of us who do not know Perl, and there are many, I suspect, on
                      this Python newsgroup, that means nothing ;-).

                      tjr




                      Comment

                      • Dang Griffith

                        #12
                        Re: Static Typing in Python

                        On Sat, 13 Mar 2004 14:59:42 +0000 (GMT), Premshree Pillai
                        <premshree_pyth on@yahoo.co.in> wrote:
                        [color=blue]
                        > --- Dang Griffith <google0@lazytw inacres.net> wrote:[color=green]
                        >> On Sat, 13 Mar 2004 07:07:50 +0000 (GMT), Premshree
                        >> Pillai
                        >> <premshree_pyth on@yahoo.co.in> wrote:
                        >>[color=darkred]
                        >> > --- Jacek Generowicz <jacek.generowi cz@cern.ch>
                        >> >Yes, I am aware that Python is dynamically typed,[/color]
                        >> and[color=darkred]
                        >> >so is Perl, right? In Perl, we have the "use strict
                        >> >vars" pragma to force variable declaration. Is[/color]
                        >> there[color=darkred]
                        >> >something like it in Python?[/color]
                        >>
                        >> No, but you can use pychecker to get similar
                        >> results.
                        >>[color=darkred]
                        >> >Don't you think forced variable declaration is an
                        >> >important requirement in a language?[/color]
                        >>
                        >> Not really. Forced variable initialization is
                        >> what's important.
                        >> Unlike C, et al, and Perl, variables don't have a
                        >> default
                        >> initial value. If you try reference a variable that
                        >> hasn't been
                        >> initialized ("bound to a value", in python lingo),
                        >> python raises a
                        >> NameError exception.
                        >>
                        >> --dang
                        >> p.s.
                        >> I know technically Perl initializes to 'undef', but
                        >> it's magically
                        >> treated as 0 or an empty string, depending on
                        >> context, so the
                        >> effect is much the same.
                        >> --
                        >> http://mail.python.org/mailman/listinfo/python-list[/color]
                        >
                        >Not forcing variable initialization does have its
                        >problems. Here's what I mean: http://www.livejournal.com/users/pre...d=53376#t53376[/color]

                        Yes, I agree. That is one reason I like Python.
                        It has forced variable *initialization *. You cannot use a variable
                        that has not been initialized. In fact, the act of initializing a
                        variable is what makes the variable exist.

                        I do not think forced *declaration* is important, except perhaps in
                        C/++/#/Java, Pascal, Ada, etc.

                        I see the typo in the pseudo-code example. PyChecker will catch that.
                        I typed in your code, converted it to Python, and ran pychecker on it:
                        Warnings...
                        bluesmoon.py:12 : Local variable (my_varaible) not used

                        --dang

                        Comment

                        • Jacek Generowicz

                          #13
                          Re: Static Typing in Python

                          Dang Griffith <google0@lazytw inacres.net> writes:
                          [color=blue][color=green]
                          > >Not forcing variable initialization does have its problems. Here's
                          > >what I mean:
                          > >http://www.livejournal.com/users/pre...d=53376#t53376[/color]
                          >
                          > Yes, I agree. That is one reason I like Python.
                          > It has forced variable *initialization *. You cannot use a variable
                          > that has not been initialized.[/color]

                          But there is nothing to prevent you from assigning to a previously
                          non-existing variable, which is what Premshree is concerned about.
                          [color=blue]
                          > In fact, the act of initializing a variable is what makes the
                          > variable exist.[/color]

                          Exactly, make a typo, and the compiler won't notice.

                          This can indeed be a minor annoyance in interactive
                          exploration. However, the development of a real program, backed by a
                          test suite, will catch the problem in no time.

                          It's just one of the thousands of possible styles of bugs that you can
                          introduce into the program ... ALL of which will be caught by a decent
                          test suite. Adding restrictions to a language just to catch one of the
                          many of styles of possible bugs is counterproducti ve. Support your
                          development with a decent test suite, and use the most flexible
                          language you can get your hands on.

                          Comment

                          • Ramon Leon Fournier

                            #14
                            Re: Static Typing in Python

                            Premshree Pillai <premshree_pyth on@yahoo.co.in> wrote:[color=blue]
                            > How do I force static typing in Python?[/color]

                            If you want

                            def foo(int bar):
                            print bar

                            then you probably want to give Pyrex a try.

                            Theek hai.

                            Ramón

                            Comment

                            • Dang Griffith

                              #15
                              Re: Static Typing in Python

                              On 15 Mar 2004 09:44:50 +0100, Jacek Generowicz
                              <jacek.generowi cz@cern.ch> wrote:
                              [color=blue]
                              >Dang Griffith <google0@lazytw inacres.net> writes:
                              >[color=green][color=darkred]
                              >> >Not forcing variable initialization does have its problems. Here's
                              >> >what I mean:
                              >> >http://www.livejournal.com/users/pre...d=53376#t53376[/color]
                              >>
                              >> Yes, I agree. That is one reason I like Python.
                              >> It has forced variable *initialization *. You cannot use a variable
                              >> that has not been initialized.[/color]
                              >
                              >But there is nothing to prevent you from assigning to a previously
                              >non-existing variable, which is what Premshree is concerned about.
                              >[color=green]
                              >> In fact, the act of initializing a variable is what makes the
                              >> variable exist.[/color]
                              >
                              >Exactly, make a typo, and the compiler won't notice.
                              >
                              >This can indeed be a minor annoyance in interactive
                              >exploration. However, the development of a real program, backed by a
                              >test suite, will catch the problem in no time.[/color]

                              Ironically, the error in the code that Premshree referenced, once
                              fixed for Python syntax, get in an infinite loop. So, a test suite
                              will not catch the problem, because the test will never finish.

                              But obviously, if one is using test-driven development, it should
                              be pretty clear which code has a problem.

                              --dang

                              Comment

                              Working...