strong/weak typing and pointers

Steven Bethard steven.bethard at gmail.com
Tue Nov 2 21:05:02 CET 2004


Diez B. Roggisch <deetsNOSPAM <at> web.de> writes:
> 
> If you do this:
> 
> "a" + 10
> 
> you end with 10 - if the string doesn't contain something as number
> interpretable, the coercion results in null.
> 
> Sure, that behaviour can be seen as overloaded, too. But overloaded
> functions usually make some sort of sense, where this technique masks
> errors by _always_ trying to interpret values as useful to every operation.

Ahh, I understand now.  I would still call this coercion with an overloaded
operator.  A horrible language decision, certainly, but not a mark of weak
typing -- note that Python can give you exactly the same behavior if you want it:

>>> class phpint(int):
... 	def __add__(self, other):
... 		try:
... 			other = int(other)
... 		except ValueError:
... 			other = 0
... 		return super(phpint, self).__add__(other)
... 	__radd__ = __add__
... 	
>>> i = phpint(10)
>>> 5 + i
15
>>> "5" + i
15
>>> "a" + i
10

This doesn't mean that Python has suddenly become a weakly-typed language.  It
just means that I've implemented some poor coercion choices in the language. =)

> I'm no native speaker, so I maybe confused the meaning of permanent. In
> german, permanent not only means that things are enduring, but also that
> things are done on regular bases: "He permanently changed his cloth." 

Ahh.  Gotcha.  I would probably say "He (regularly changed/repeatedly
changed/used to change) his clothes."  Thanks for the clarification.

> What I wanted to say is that php uses coercions or overloaded operators for
> nearly everything. Sometimes this is totally silent, sometimes nothing
> happens and a warning is issued - which might be configured to be treated
> as an actual error, I'm not sure about that.

Totally clear now, thanks.  Basically you would say that the more implicit
coercions a language performs, the more weakly typed it is.  This diverges from
the common use of the terms strong and weak typing in the PL literature, which
is why I was confused.

> So while there migth be in fact type information present, it's rarely used -
> which I consider as beeing weak.

Well, the type information is probably used all the time (I would't be surprised
if somewhere in the PHP internals something like my __add__ method above was
defined), but it's used implicitly, so the programmer might never see it.

Steve





More information about the Python-list mailing list