strong/weak - dynamic/static [Was: Getting started]

sismex01 at sismex01 at
Thu Sep 19 16:09:19 CEST 2002

> Sorry to go off at a tangent ...
> "Lance" <lbrannma at> writes:
> > Hi All,
> > 
> > Last night I attended a Weak typing, C++ Templates, and 
> > Python talk by Bruce Eckel. It was great, my introduction
> > to Python.
> > 
> > I'm sold on Python and weak typing. I want to write a 
> > graphics module that will link to a C application,
> > permitting scatter plots, bar charts, log scales, etc.
> > I suppose the Python Imaging Library will be used for this
> > purpose.

Sorry pal, someone must have sold you some ranch land
in Florida also. ;-)

Python is definitely NOT weakly typed, it's dynamicly-strong
typed. A variable, being only a "reference to *any* object"
may refer to any object, but that object (see the difference?)
may not change it's functionality or type during runtime.

So, being strong typed, you cannot "cast" an object to another
type of object, like in C, you can do some strange contortions
to get what you want (forcing the compiler to do what you need,
when doing it in ... assembler, for example, would be quite
direct. Obtain the first byte from a double number,
for example:

	unsigned char c = *(char*)(&somefloat);

Now, look at this variable "unsigned char c". It can never,
ever, during the execution of your program, become anything else.
Why? Because it's storage was staticly allocated during
runtime, it doesn't hold a "reference to an unsigned_char object",
it actually *is* the address of the byte where an unsigned-char
is held;  although it obtained it's value through some ugly
type contortions.

So, you see, in C (a strong, static typed language) I obtained
a char from a floating-point number; not the string representation,
just the first byte.

On the other hand, in Python, we have:

>>> num = 3.1315

"num" contains a reference to the floating point object 3.1415,
which exists "somewhere in memory" (although you mustn't care
about that).

If you do the following:

>>> num = "3.1415"

Now, "num" has a reference to an entirely different object,
a string which contains "3.1415", which is only a string,
it doesn't matter if it "looks" like a number.

If you should do something like:

>>> num += 10
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects

See? Python won't let you do operations on an object which
correspond to a different object type. This is what it means
for "strong typing". But, since you can change a variable's
binding at runtime, it's dynamicly typed.

On the other hand, say, VBScript (yuck), you can do the

DIM a,b
a = "Howdy!"
b = 3.1415

You have two variables, "a" which is a string, and "b" which
is a floating-point number. You can do operations such as:

a = a & b

"&" is a string concatenation operator, it's used to concatenate
strings; but since you're passing a floating-point number,
it automagically converts it to string and then concatenates
it. This is what weakly-typed languages to, they convert
objects to different types depending on the operation you're
trying to do with them.

VBScript is weak typed; Perl is notoriously weak typed
(everything's a string to Perl). Other scripting languages
are weak typed.

This question comes up every now and then, I think I'll
save this post, and repost it when the need comes. :-)

> A quick google search suggests that Bruce Eckel really claims that
> python is weakly typed (at least more weakly typed than C++).

This is Phalse (i.e., "very false"). An object in C++ can be
cast to a different type, same as with Java; in Python you
can do no such contortions (there's no need though).

The need in C++ and Java to have type casts comes from the
fact that variable's are staticly typed, so you can't bind
them to a different type of object than what they were
declared with, so you need to fix it up, by saying to the
program "you're receiving a X pointer, but it's not really
pointing to an X, but to a Y, and you're Y, so everythin's



La informacion contenida en este mensaje es confidencial y restringida y
esta destinada unicamente para el uso de la persona arriba indicada, Esta
comunicacion representa la opinion personal del remitente y no refleja
necesariamente la opinion de la Compañia. Se le notifica que esta
estrictamente prohibida cualquier difusion, distribucion o copia de este
mensaje. Si ha recibido esta comunicacion o copia de este mensaje por error,
o si hay problemas en la transmision, favor de comunicarse con el remitente.

Todo el correo electrónico enviado para o desde esta dirección será
procesado por el sistema de correo corporativo de HEB. Tal correo
electrónico esta sujeto a ser almacenado y puede ser revisado por alguien
ajeno al recipiente autorizado con el propósito de monitorear que se cumplan
las normas de seguridad de la empresa.

More information about the Python-list mailing list