strong/weak typing and pointers

Alex Martelli aleaxit at yahoo.com
Thu Oct 28 15:40:57 EDT 2004


JCM <joshway_without_spam at myway.com> wrote:

> Gabriel Zachmann <zach at cs.uni-bonn.de> wrote:
> 
> > Is it correct to say that strong/weak typing does not make a difference
> > if one does not use any pointers (or adress-taking operator)?
> 
> You'll find a lack of consensus here on what's meant by "strong/weak
> typing".  In Python there's no way to re-interpret the bits of a value
> as if they were a different type.  For example, code like this is
> impossible in Python:
> 
>   float x = 2.5;
>   printf("%d\n", *(int*)&x);

True, but module struct lets you get the same effect, though the 4 bytes
get copied, not 'reinterpreted in place'.

 
> > 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++?
> 
> If I understand your question, I believe not; because Python doesn't
> provide the low-level operators that would be necessary for it.

Well... what about something like:

std::list<int> a, b;

...

a[2] = 45;
b = a;
b[2] = 23;

In C++, a[2] is still 45, because 'b = a;' COPIED the whole list over.

A similar case in Python would make no implicit copies, just give an
additional name 'b' to the same object which 'a' names, so assigning to
b[2] would also change a[2].  Nothing to do with dynamic vs static
typing, of course, because e.g. Java would work like Python here.

I've found this one tidbit to be the single biggest stumbling block for
experienced C or C++ programmers learning Java or Python.  "without
references" isn't really true, because (in Python and Java) a and b
_are_ 'references' (aka names) to the same object -- but then, neither
in Java nor Python can you say that a name _isn't_ ``a reference''...
names always reference objects... ((Java makes exceptions to this rule
for some lowlevel types such as ints, Python doesn't)).

Templates may be another case in which C++ might act one way, and Java
and Python the other way, and may be more relevant to type issues.

E.g.,

template<typename T>
T foo(const T& bar)
{
    static T baz;
    T temp = baz;
    baz = bar;
    return temp;
}

now, if you make a series of calls such as foo(1), foo(1.2), foo(2),
foo(3.4), you should get as results 0, 0.0, 1, and 1.2 -- there are two
'foo's, one instantiated for T being int, another one for T being
double, so the 'delay register' baz also exists in two incarnations.

In the Python rough equivalent:

def foo(bar, _baz=[None]):
    temp = _baz.pop()
    _baz.append(bar)
    return temp

(and the Java equivalent, too, with everything declared as Object to be
"generic"), the same calls would give None, 1, 1.2, 2 -- there is a
single 'incarnation' of foo, a single 'delay register' _baz.  (Not sure
which way Java 1.5's generics go wrt statics; I'd expect the C++ way).


Not sure I've gotten the gist of what the OP was asking about, though.

Alex



More information about the Python-list mailing list