Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > strong/weak typing and pointers

Reply
Thread Tools

strong/weak typing and pointers

 
 
Gabriel Zachmann
Guest
Posts: n/a
 
      10-28-2004

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

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 would appreciate all insights or pointers to literature.

TIA,
gabriel.

--
/-------------------------------------------------------------------------\
| We act as though comfort and luxury |
| were the chief requirements of life, |
| when all that we need to make us happy |
| is something to be enthusiastic about. (Einstein) |
+-------------------------------------------------------------------------+
| __@/' www.gabrielzachmann.org |
\-------------------------------------------------------------------------/
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-28-2004
> 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)?


It seems that you mistake strong/weak typing with static/dynamic typing - a
completely different beast.

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.

And pointers are not evil in themselves - the are a neccessity to create
recursive structures. But deliberately casting pointers can be very harmful
- a reason why its forbidden in languages like java and AFAIK ada.

> 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.

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
 
 
 
JCM
Guest
Posts: n/a
 
      10-28-2004
Gabriel Zachmann <> 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);

> 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.
 
Reply With Quote
 
Andrea Griffini
Guest
Posts: n/a
 
      10-28-2004
On Thu, 28 Oct 2004 18:34:12 +0200, "Diez B. Roggisch"
<> 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)?

>
>It seems that you mistake strong/weak typing with static/dynamic typing - a
>completely different beast.
>
>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.


You didn't mention C++. Try this ...

std::string s = "Wow";
s += 3.141592654; // Perfectly valid
s = 3.141592654; // Also valid

Andrea
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      10-28-2004
On 2004-10-28, Diez B. Roggisch <> wrote:

> It seems that you mistake strong/weak typing with
> static/dynamic typing - a completely different beast.
>
> 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.


"1" is a pointer to a char.

> And even though C is statically typed, it won't complain


That's because <pointer> + <integer> has a well-defined meaning
in C -- just like <float> + <integer> does in Python (and in C).

> - you just end up with an unexpected result.


Only people who don't know how C pointer arithmatic works will
get unexpected results. [That's probably a shockingly high
percentage of C programmers.]

--
Grant Edwards grante Yow! I brought my BOWLING
at BALL -- and some DRUGS!!
visi.com
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      10-28-2004
JCM <> wrote:

> Gabriel Zachmann <> 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
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-28-2004
On Thu, 28 Oct 2004 18:34:12 +0200, Diez B. Roggisch <> 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!
 
Reply With Quote
 
Mel Wilson
Guest
Posts: n/a
 
      10-28-2004
In article <>,
Gabriel Zachmann <> 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)?


One effect of weak typing is to put more reliance on
operators. In Perl, for instance the string operator `lt`
does a string compare to find that "10" is less than 2
(lexically) and the numeric operator `<` finds that "10" is
not less than 2 (numerically). Nothing to do with pointers
at all.

Regards. Mel.
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      10-29-2004
Gabriel Zachmann 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)?
>
> 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++?


Here's a trivial example that is almost identical in Python and C/C++ but
gives totally different results. In a weakly typed language such as C or
C++:

#include <stdio.h>

int main(int argc, char**argv)
{
float f = 3;
printf("value is %d", f);
}

I get the output (you may get different results):

value is 0

In a fairly strongly typed language such as Python:

>>> f = 3.0
>>> print "value is %d" % f

value is 3

In a really strongly typed language I would expect an exception to
be thrown.
 
Reply With Quote
 
Oliver Fromme
Guest
Posts: n/a
 
      10-29-2004
Diez B. Roggisch <> wrote:
> And pointers are not evil in themselves - the are a neccessity to create
> recursive structures. But deliberately casting pointers can be very harmful
> - a reason why its forbidden in languages like java and AFAIK ada.


I agree. A language worth mentioning in this context might
be Cyclone. It's derived from C (and still has very much in
common with it, so it's easy to port C programs to Cyclone).
The difference is that "safe" features have been added to the
language. For example, you can't do arbitrary type casts on
pointers anymore, and you can't access strings (or other
allocated memory) beyond their end. The ultimate goal of
Cyclone is to make it impossible for programs to crash or
have security holes caused by buffer overflows or similar.

Furthermore, Cyclone provides interesting features, such as
tagged unions, parametric polymorphism, pattern matching,
exceptions, even a somewhat limited implementation of type
inference.

http://www.research.att.com/projects/cyclone/

Best regards
Oliver

--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''
(E. A. Poe)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Typing to Nullable (Of Date) vs typing to Date JimLad ASP .Net 0 01-26-2010 07:54 PM
typing location changes when I am typing. Ed Computer Support 5 11-11-2006 12:51 AM
Static Typing Where Possible and Dynamic Typing When Needed vladare Ruby 0 07-11-2005 11:54 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57