Python Strings

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Wed Sep 6 06:07:24 EDT 2000


Jonadab the Unsightly One wrote in comp.lang.python:
> Erik Max Francis <max at alcyone.com> wrote:
> 
> > That's sort of the flip side of weakly-typed languages; if they're
> > weakly typed then you have to play games when you _do_ need to know the
> > type of an object.
> 
> I guess that I would still consider a language sufficiently
> "loosely typed"[1] if you can always determine the type of 
> an item, as long as you can proceed to assign an entirely 
> different item to the same variable a moment later.

Ah. But that definition is a bit unclear; in Python, a variable is only
a reference to some object, a name for that object. *Variables don't have
type, the object has*. Now you can simply assign a completely new object
to that name, but no types changed there - the variable simply refers to
another object with another type. 

Objects can't change type, immutable objects can't even change value.

So in Python you can always find out the type of the object a variable
refers to, and still assign another object to the variable, and I'd still
call it strictly typed.

The problem is that assignment is different from other languages.


> What I really like is the ability to have an object
> automatically do the appropriate thing with what you
> give it, regardless of what kind of thingy that is.
> This is only possible when you can give it any kind
> of thing that might be appropriate.  And in the general
> case, that means you have to be able to give the object
> any kind of thing there is -- a routine, a string,
> a number, or whatever -- and have it be able to know 
> what it has (if it needs to know; sometimes it 
> doesn't need to know, of course, if it's only passing
> the thingy on to some other object).

In Python, it doesn't usually need to know - it just tries to use it the
way it wants to, and that will throw an exception if it doesn't work.

def adder(x,y):
   return x+y

The function just assumes that x and y can be added together, whatever they
are. Otherwise the appropriate exception is raised.

And it *couldn't* work by checking what kind of argument it got - maybe
x and y are instances of a class WereInteger, that wasn't around when adder
was coded, and which only allows addition during a full moon...

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
"This gubblick contains many nonsklarkish English flutzpahs, but the
 overall pluggandisp can be glorked from context"  (David Moser)



More information about the Python-list mailing list