strong/weak typing and pointers

Jorgen Grahn jgrahn-nntq at algonet.se
Thu Oct 28 15:51:31 EDT 2004


On Thu, 28 Oct 2004 18:34:12 +0200, Diez B. Roggisch <deetsNOSPAM at web.de> wrote:
...
> Python is in fact strong typed - in opposition to php or perl or even C,
> this won't work:
> 
> a = "1" + 2
> 
> as "1" is a string and 2 an integer. And even though C is statically typed,
> it won't complain - you just end up with an unexpected result.

[slightly offtopic defense of C and C++]

That's true only if a is a 'char *' of course (and if you didn't expect
this unexpected result ;-).

In C++ 'char *' would have been invalid, but not 'const char *' or
(and this is worse) 'std::string'.

> And pointers are not evil in themselves - the are a neccessity to create
> recursive structures.

I'd say they are neccessary, period.  But note that I count what Java and
Python call "references" as pointers ...

> But deliberately casting pointers can be very harmful 
> - a reason why its forbidden in languages like java and AFAIK ada.

Yes; lots of casts in C code (or worse, in C++ code) is a very, very bad
sign. Note though, that in the absense of casts, C and in particular C++ are
pretty strongly typed for pointers. Strongly enough to keep me happy, at
least.

>> More concretely, I am thinking particularly of Python vs C++.
>> So, are there any examples (without pointers, references, or
>> adress-taking), which would have a different result in Python and in C++?
> 
> I have difficulties to understand what you want here. Please elaborate.

I think he means the static/dynamic typing, and if it makes a difference in
a simple C and a simple Python program, if we pretend that all names are
just "variables".  Hard to come up with a meaningful answer, but how about:

a = 2                         const int a = 2;
if something_rare_happens:    if(something_rare_happens) {
    return b                      return b;
a = 'hugo'                    }
                              std::string a("hugo");
bar(a)                        bar(a);

In the Python program, we might clobber 'a' by accidentaly reusing its name
for something of a different type.  C++ is stricter about this (doesn't
allow the construct above, in fact) and you can look at the code
(statically) to see which names are in scope and which are not.

In Python, you can forget to give 'b' a value, and not notice until that
code executes. You can in C++ too, and the runtime effects will be more
subtle but worse. The compiler is more likely to catch pure typos, though.

In Python, it is often hard to look at a function such as 'bar' and say you
know it is always called with an integer argument, or a string, or a 'Foo'
object. It's not even enough to look at all places where 'bar' is called,
because the type of b may depend on the phase of the moon or other dynamic
things. In C++ they compiler makes the guarantees, unless someone has
willfully bypassed the type system.

Is all this caused by the static/dynamic typing difference? No, but it
certainly has to do with it. Both languages have made a decision here, and
that of course works together with the rest of the language design. Python
doesn't /have/ to declare variables and parameters to give them a type, so
Guido said you don't have to, and let functions take all kinds of flexible
arguments.  C++ had to have declarations/definitions, so Bjarne used them to
add the 'const' keyword, to give values a scope and making it well-defined
when objects are destroyed. And so on.

For what it's worth, I think both kinds of typing are interesting and useful
tools. Neither of them are obsolete or inferior; neither of them will
disappear in the next ten years.

/Jorgen

-- 
  // Jorgen Grahn <jgrahn@       Ph'nglui mglw'nafh Cthulhu
\X/                algonet.se>   R'lyeh wgah'nagl fhtagn!



More information about the Python-list mailing list