Is Python type safe?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • srijit@yahoo.com

    Is Python type safe?

    Hello,
    I would like to know the definition of type safe and whether Python
    can be considered as a type safe language. Similarly are Java, C# or
    C++ type safe?

    Regards,
    Srijit
  • Roy Smith

    #2
    Re: Is Python type safe?

    In article <221d8dbe.04031 61043.52406195@ posting.google. com>,
    srijit@yahoo.co m wrote:
    [color=blue]
    > Hello,
    > I would like to know the definition of type safe and whether Python
    > can be considered as a type safe language. Similarly are Java, C# or
    > C++ type safe?
    >
    > Regards,
    > Srijit[/color]

    You tell me what you mean by "type safe", and I'll tell you if those
    languages meet that definition.

    C++ and Java both have the concept of declaring variables to hold a
    certain type of data. I don't know anything about C#, but I'll assume I
    can lump it into the C++/Java camp.

    Python on the other hand, carries the type information along with the
    data, not with the variable (container) that holds the data.

    Neither C++ nor Python will let you add the integer 3 to the string
    "four", but use different mechanisms to prevent it. Oddly enough, Java
    (in a perl-like, but admittedly convenient, stab at automagic
    polymorphism) will let you add them. No clue what C# does.

    Which is type safe and which isn't? Well, I think we're back to my
    first statement, otherwise we quickly get into playing three blind men
    and the elephant.

    Comment

    • Peter Hansen

      #3
      Re: Is Python type safe?

      Roy Smith wrote:
      [color=blue]
      > Neither C++ nor Python will let you add the integer 3 to the string
      > "four", but use different mechanisms to prevent it. Oddly enough, Java
      > (in a perl-like, but admittedly convenient, stab at automagic
      > polymorphism) will let you add them. No clue what C# does.[/color]

      It's been a while since I Java-ed, but doesn't it "append" them, as in
      convert the integer to a string and then join the two strings, rather
      than "add" them (which I take to mean "calculate the sum of")?

      (That's probably what you meant, but I'm just checking. I'd be
      surprised to find that Java actually performed the string-to-value
      conversion implicitly.)

      -Peter

      Comment

      • Skip Montanaro

        #4
        Re: Is Python type safe?


        srijit> I would like to know the definition of type safe

        Try reading



        (found by googling for "wikipedia type safety").

        srijit> and whether Python can be considered as a type safe
        srijit> language. Similarly are Java, C# or C++ type safe?

        Python: yes. I don't know about Java or C# but my guess is that since C++
        has casts you can make it do some type unsafe things.

        Skip

        Comment

        • Mathias Waack

          #5
          Re: Is Python type safe?

          Roy Smith wrote:[color=blue]
          > Neither C++ nor Python will let you add the integer 3 to the string
          > "four", but use different mechanisms to prevent it. Oddly enough,
          > Java (in a perl-like, but admittedly convenient, stab at automagic
          > polymorphism) will let you add them.[/color]

          Thats not true, Java doesn't let you add an integer to a string. It
          only looks like that because of implicit type conversion. BTW the
          same holds for C++ if you are using a string type which allows this
          conversion.

          Mathias

          Comment

          • Roy Smith

            #6
            Re: Is Python type safe?

            In article <QuJ5c.3008$G3. 27398@localhost >,
            Peter Hansen <peter@engcorp. com> wrote:
            [color=blue]
            > Roy Smith wrote:
            >[color=green]
            > > Neither C++ nor Python will let you add the integer 3 to the string
            > > "four", but use different mechanisms to prevent it. Oddly enough, Java
            > > (in a perl-like, but admittedly convenient, stab at automagic
            > > polymorphism) will let you add them. No clue what C# does.[/color]
            >
            > It's been a while since I Java-ed, but doesn't it "append" them, as in
            > convert the integer to a string and then join the two strings, rather
            > than "add" them (which I take to mean "calculate the sum of")?
            >
            > (That's probably what you meant, but I'm just checking. I'd be
            > surprised to find that Java actually performed the string-to-value
            > conversion implicitly.)
            >
            > -Peter[/color]

            Yes, "four" + 3 ==> "four3"

            I could imagine a language where "4" + 3 ==> 7 (actually, I don't have
            to imagine it, all I need do is look at perl). But, a language where
            "four" + 3 ==> 7 would be pretty bizarre :-)

            Comment

            • Skip Montanaro

              #7
              Re: Is Python type safe?


              Skip> Python: yes. I don't know about Java or C# but my guess is that
              Skip> since C++ has casts you can make it do some type unsafe things.

              Sorry to follow up to my own post, but unions also create type unsafety.
              Unlike Pascal's records (remember them?), C/C++ unions are not
              discriminated, so you can write values into the union using one slot and
              read using another:

              #include <stdio.h>
              int main(int argc, char *argv[])
              {
              union {
              float f;
              int i;
              } x;

              x.f = 3.14159;
              fprintf(stderr, "%x\n", x.i);
              }

              (pause while Skip reboots the one neuron that knows anything about unions
              and compiles this to see if he's got it right...) which produces:

              % make foo
              cc -c -o foo.o foo.c
              cc foo.o -o foo
              % ./foo
              40490fd0

              Skip





              Comment

              • Roy Smith

                #8
                Re: Is Python type safe?

                In article <mailman.49.107 9471195.742.pyt hon-list@python.org >,
                Skip Montanaro <skip@pobox.com > wrote:
                [color=blue]
                > Skip> Python: yes. I don't know about Java or C# but my guess is that
                > Skip> since C++ has casts you can make it do some type unsafe things.
                >
                > Sorry to follow up to my own post, but unions also create type unsafety.
                > Unlike Pascal's records (remember them?), C/C++ unions are not
                > discriminated, so you can write values into the union using one slot and
                > read using another:
                >
                > #include <stdio.h>
                > int main(int argc, char *argv[])
                > {
                > union {
                > float f;
                > int i;
                > } x;
                >
                > x.f = 3.14159;
                > fprintf(stderr, "%x\n", x.i);
                > }[/color]

                Of course you can. Given C's roots as a high level assembly language,
                why would you expect anything else?

                I was once asked on an interview how I would tell, inside of a C
                program, if I was on a big endian or a small endian machine. I said I'd
                create a union of a long and a char[4], set the long equal to 1, and see
                which char it showed up in. The interviewer looked shocked, thought
                about it for a while, and finally said something like, "yeah, I guess
                that would work".

                To this day, I have no idea what other plan he had in mind.

                Comment

                • Mark Jackson

                  #9
                  Re: Is Python type safe?

                  Roy Smith <roy@panix.co m> writes:[color=blue]
                  > In article <QuJ5c.3008$G3. 27398@localhost >,
                  > Peter Hansen <peter@engcorp. com> wrote:
                  >[color=green]
                  > > Roy Smith wrote:
                  > >[color=darkred]
                  > > > Neither C++ nor Python will let you add the integer 3 to the string
                  > > > "four", but use different mechanisms to prevent it. Oddly enough, Java
                  > > > (in a perl-like, but admittedly convenient, stab at automagic
                  > > > polymorphism) will let you add them. No clue what C# does.[/color]
                  > >
                  > > It's been a while since I Java-ed, but doesn't it "append" them, as in
                  > > convert the integer to a string and then join the two strings, rather
                  > > than "add" them (which I take to mean "calculate the sum of")?
                  > >
                  > > (That's probably what you meant, but I'm just checking. I'd be
                  > > surprised to find that Java actually performed the string-to-value
                  > > conversion implicitly.)
                  > >
                  > > -Peter[/color]
                  >
                  > Yes, "four" + 3 ==> "four3"
                  >
                  > I could imagine a language where "4" + 3 ==> 7 (actually, I don't have
                  > to imagine it, all I need do is look at perl). But, a language where
                  > "four" + 3 ==> 7 would be pretty bizarre :-)[/color]

                  And would the result of "quatre" + 7, say, depend on the locale setting?

                  --
                  Mark Jackson - http://www.alumni.caltech.edu/~mjackson
                  The supreme misfortune is when theory outstrips performance.
                  - Leonardo da Vinci


                  Comment

                  • Nicolas Fleury

                    #10
                    Re: Is Python type safe?

                    Roy Smith wrote:[color=blue]
                    > I was once asked on an interview how I would tell, inside of a C
                    > program, if I was on a big endian or a small endian machine. I said I'd
                    > create a union of a long and a char[4], set the long equal to 1, and see
                    > which char it showed up in. The interviewer looked shocked, thought
                    > about it for a while, and finally said something like, "yeah, I guess
                    > that would work".
                    >
                    > To this day, I have no idea what other plan he had in mind.[/color]

                    Maybe the guy was thinking about writing to a file, but your way using a
                    short is the best I know of...

                    Comment

                    • Aahz

                      #11
                      Re: Is Python type safe?

                      In article <536ki1-lp3.ln1@valpo.d e>, Mathias Waack <M.Waack@gmx.de > wrote:[color=blue]
                      >Roy Smith wrote:[color=green]
                      >>
                      >> Neither C++ nor Python will let you add the integer 3 to the string
                      >> "four", but use different mechanisms to prevent it. Oddly enough,
                      >> Java (in a perl-like, but admittedly convenient, stab at automagic
                      >> polymorphism) will let you add them.[/color]
                      >
                      >Thats not true, Java doesn't let you add an integer to a string. It
                      >only looks like that because of implicit type conversion. BTW the
                      >same holds for C++ if you are using a string type which allows this
                      >conversion.[/color]

                      Ditto Python, but Python by default doesn't do implicit type conversion
                      and it's strongly discouraged.
                      --
                      Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

                      "usenet imitates usenet" --Darkhawk

                      Comment

                      • Erik Max Francis

                        #12
                        Re: Is Python type safe?

                        srijit@yahoo.co m wrote:
                        [color=blue]
                        > I would like to know the definition of type safe and whether Python
                        > can be considered as a type safe language. Similarly are Java, C# or
                        > C++ type safe?[/color]

                        It depends what you mean by "type safe." I've heard two broad
                        definitions commonly used:

                        - objects have an intrinsic type which dictates how they behave, objects
                        define how operations on them behave, rather than operations defining
                        how untyped objects behave

                        - the type system in the language cannot be thwarted through unsafe
                        casts and the like

                        Python and Java meet both definitions; I'm not sure about C# since I
                        know there are unsafe operations supported within it (unlike Java) but I
                        don't know if they allow for defeating the type system. C++ probably
                        meets the first definition, but definitely doesn't meet the second. C
                        probably doesn't meet either. A weakly-typed language like Perl doesn't
                        meet the first definition either.

                        --
                        __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                        / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
                        \__/ Here day fights with night.
                        -- (the last words of Victor Hugo)

                        Comment

                        • Greg Ewing (using news.cis.dfn.de)

                          #13
                          Re: Is Python type safe?

                          Roy Smith wrote:[color=blue]
                          > Neither C++ nor Python will let you add the integer 3 to the string
                          > "four",[/color]

                          Actually, C++ (and C) will:

                          cosc353% cat foo.c
                          #include <stdio.h>
                          main() {
                          const char *s = "four" + 3;
                          printf("%s\n", s);
                          }
                          cosc353% g++ foo.c -o foo
                          cosc353% foo
                          r

                          --
                          Greg Ewing, Computer Science Dept,
                          University of Canterbury,
                          Christchurch, New Zealand


                          Comment

                          • Roy Smith

                            #14
                            Re: Is Python type safe?

                            In article <c388ik$24slfg$ 1@ID-169208.news.uni-berlin.de>,
                            "Greg Ewing (using news.cis.dfn.de )" <wmwd2zz02@snea kemail.com> wrote:
                            [color=blue]
                            > Roy Smith wrote:[color=green]
                            > > Neither C++ nor Python will let you add the integer 3 to the string
                            > > "four",[/color]
                            >
                            > Actually, C++ (and C) will:
                            >
                            > cosc353% cat foo.c
                            > #include <stdio.h>
                            > main() {
                            > const char *s = "four" + 3;
                            > printf("%s\n", s);
                            > }
                            > cosc353% g++ foo.c -o foo
                            > cosc353% foo
                            > r[/color]

                            I can't believe I forgot about that! Of course, if we're going to play
                            those games, there's always:

                            Roy-Smiths-Computer:play$ cat sub.c
                            main ()
                            {
                            printf ("%d\n", "eight" - "sixteen");
                            exit (0);
                            }
                            Roy-Smiths-Computer:play$ ./sub
                            -8

                            Comment

                            • Erik Max Francis

                              #15
                              Re: Is Python type safe?

                              Roy Smith wrote:
                              [color=blue]
                              > I can't believe I forgot about that![/color]

                              Well, pointer arithmetic is certainly a different class of operations
                              than was being talked about here, so literally it's true that you can
                              perform the addition but semantically it has a totally different and
                              unrelated meaning than addition between strings and integers in other
                              languages.
                              [color=blue]
                              > Of course, if we're going to
                              > play
                              > those games, there's always:
                              >
                              > Roy-Smiths-Computer:play$ cat sub.c
                              > main ()
                              > {
                              > printf ("%d\n", "eight" - "sixteen");
                              > exit (0);
                              > }
                              > Roy-Smiths-Computer:play$ ./sub
                              > -8[/color]

                              Cute, but this actually invokes undefined behavior. Pointer difference
                              is only meaningful when you're subtracting two pointers that both
                              reference the same (possibly aggregate) object (or one past the end of
                              the array). It happens to give some weird number here; according to the
                              Standard the program could reasonably crash (and on, say, segmented
                              architectures, it's far more likely something bizarre would happen).

                              --
                              __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                              / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
                              \__/ Liberty without learning is always in peril; learning without
                              liberty is always in vain. -- John F. Kennedy

                              Comment

                              Working...