473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jul 18 '05 #1
18 7061
In article <22**************************@posting.google.com >,
sr****@yahoo.com wrote:
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


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.
Jul 18 '05 #2
Roy Smith wrote:
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.


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
Jul 18 '05 #3

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

Try reading

http://en.wikipedia.org/wiki/Talk:Datatype

(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

Jul 18 '05 #4
Roy Smith wrote:
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.


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
Jul 18 '05 #5
In article <QuJ5c.3008$G3.27398@localhost>,
Peter Hansen <pe***@engcorp.com> wrote:
Roy Smith wrote:
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.


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


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 :-)
Jul 18 '05 #6

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

Jul 18 '05 #7
In article <ma***********************************@python.org> ,
Skip Montanaro <sk**@pobox.com> wrote:
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);
}


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.
Jul 18 '05 #8
Roy Smith <ro*@panix.com> writes:
In article <QuJ5c.3008$G3.27398@localhost>,
Peter Hansen <pe***@engcorp.com> wrote:
Roy Smith wrote:
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.


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


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 :-)


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
Jul 18 '05 #9
Roy Smith wrote:
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.


Maybe the guy was thinking about writing to a file, but your way using a
short is the best I know of...
Jul 18 '05 #10
In article <53************@valpo.de>, Mathias Waack <M.*****@gmx.de> wrote:
Roy Smith wrote:

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.


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.


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

"usenet imitates usenet" --Darkhawk
Jul 18 '05 #11
sr****@yahoo.com wrote:
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?


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 && ma*@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)
Jul 18 '05 #12
Roy Smith wrote:
Neither C++ nor Python will let you add the integer 3 to the string
"four",


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
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #13
In article <c3*************@ID-169208.news.uni-berlin.de>,
"Greg Ewing (using news.cis.dfn.de)" <wm*******@sneakemail.com> wrote:
Roy Smith wrote:
Neither C++ nor Python will let you add the integer 3 to the string
"four",


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


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
Jul 18 '05 #14
Roy Smith wrote:
I can't believe I forgot about that!
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.
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


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 && ma*@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
Jul 18 '05 #15
Sorry to follow up to my own post, but unions also create type
unsafety.


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

Nope. But the user asked if C++ was type safe. The presence of unions
indicates that it's not, at least in some situations.

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

Sure. There are plenty of reasons to use unions. They can be a
double-edged sword though.

Skip

Jul 18 '05 #16
Roy Smith wrote:
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.

Here's my guess:

long i = 0;
*(short*)&i = 1;

if (i == 1) { printf("It's whatever one Intel uses.\n"); }
else { printf("It's the other one.\n"); }
--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
Jul 18 '05 #17
Carl Banks wrote:
Here's my guess:

long i = 0;
*(short*)&i = 1;

if (i == 1) { printf("It's whatever one Intel uses.\n"); }
else { printf("It's the other one.\n"); }


You meant char, not short.

And none of the solutions so far acknowledge the fact that none of these
types have absolute sizes as defined in the Standard. If I were
conducting the interview, I'd take points off if that weren't at least
acknowledged.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Love is when you wake up in the morning and have a big smile.
-- Anggun
Jul 18 '05 #18
In article <ro***********************@reader1.panix.com>, Roy Smith wrote:
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.


unsigned short x = 1;
if (x == ntohs(x)) printf("Network byte order!\n");
else printf("Something else!\n");

Joe
Jul 18 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Nitin Bhardwaj | last post by:
Hi all, It is said that C++ is a strongly typed language and thus a type-safe language (unlike C). So how does one explain the following behaviour : int main(void) { char *p = NULL; p = "A...
3
by: karthick.ramachandran | last post by:
Hi, I was just reading this article http://www.netobjectives.com/resources/downloads/Best_Practices_CSharp_Delegates.pdf In which the author had mentioned <quote> Delegates would appear...
2
by: Jeff Chan | last post by:
I have read the documentation from msdn to try to understanding the concept of "Type safe". Would someone give me an example of code segment illustrating what is *Non* type safe? Many Thanks,...
1
by: Antony Sequeira | last post by:
I wanted to make a type safe array slice method (at least for single dimension arrays) Here is what I have public static <T> T arraySlice(T src, int offset, int length) { int dim = new int...
2
by: Amit Bansal \(MCT, MCSD.NET\) | last post by:
delegates are type safe. what exacly is meant by type safe ?? any good document explaining the benefits of delegates.
1
by: Macca | last post by:
Hi, I want to have an array/arraylist of a user defined class. This class just holds a number of queue collections. For efficiency I want my arraylist/array to be type safe to avoid...
21
by: Chad | last post by:
Okay, so like recently the whole idea of using a Union in C finally sunk into my skull. Seriously, I think it probably took me 2 years to catch on what a Union really is. Belated, I mentioned this...
4
by: George2 | last post by:
Hello everyone, My question is whether my understanding of the solution to type safe issue in CoCreateInstance is correct. There is type safe issue in CoCreateInstance, ...
1
by: Philss100 | last post by:
Hi, I have been in the process of updating my code with security methods, and I've been learning this from http://msdn.microsoft.com/en-us/library/ms998258.aspx#pagguidelines0001_authorization (or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.