473,605 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strong/weak typing and pointers


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) |
+-------------------------------------------------------------------------+
| za**@cs.uni-bonn.de __@/' www.gabrielzachmann.org |
\-------------------------------------------------------------------------/
Jul 18 '05 #1
94 4674
> 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
Jul 18 '05 #2
JCM
Gabriel Zachmann <za**@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);
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.
Jul 18 '05 #3
On Thu, 28 Oct 2004 18:34:12 +0200, "Diez B. Roggisch"
<de*********@we b.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)?


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
Jul 18 '05 #4
On 2004-10-28, Diez B. Roggisch <de*********@we b.de> 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
Jul 18 '05 #5
JCM <jo************ ******@myway.co m> wrote:
Gabriel Zachmann <za**@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<typena me 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
Jul 18 '05 #6
On Thu, 28 Oct 2004 18:34:12 +0200, Diez B. Roggisch <de*********@we b.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_ra re_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!
Jul 18 '05 #7
In article <sl************ *****@fuji.info rmatik.uni-bonn.de>,
Gabriel Zachmann <za**@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)?


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.
Jul 18 '05 #8
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.
Jul 18 '05 #9
Diez B. Roggisch <de*********@we b.de> 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)
Jul 18 '05 #10

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

Similar topics

53
9920
by: dterrors | last post by:
Will php 6 do strong typing and/or namespaces? I was shocked to find out today that there are some people who actually argue that weak typing is somehow better. I didn't even know there was a debate. Shocking. That's like arguing that square tires are better than round. Anyway if anyone knows that php is going to intentionally stay weak in the future let me know so I can jump ship, thanks,
0
8411
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8281
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5885
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5444
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1270
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.