learnpython.org - an online interactive Python tutorial

Heiko Wundram modelnine at modelnine.org
Thu Apr 21 09:30:45 CEST 2011


Am 21.04.2011 09:19, schrieb Chris Angelico:
> On Thu, Apr 21, 2011 at 5:10 PM, Algis Kabaila <akabaila at pcug.org.au> wrote:
>> False: Python IS strongly typed, without doubt (though the
>> variables are not explicitly declared.)
> 
> Strongly duck-typed though. If I create a class that has all the right
> members, it can simultaneously be a file, an iterable, a database, and
> probably even a web browser if it feels like it. Is that strong typing
> or not?

Yes, that's strong typing, because your class only works in those
contexts that you "explicitly" allow it to work in (through implementing
an interface, be it an iterator, a file, etc.), independent of
"duck-typing" (which is pretty much described by the term
interface-based typing IMHO).

The difference between strong typing and weak typing is best described by:

Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
[GCC 4.3.4 20090804 (release) 1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+'2'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>

which means that the interface for implementing "+" on the input types
"int" and "str" isn't implemented (i.e., TypeError). Weakly typed
languages allow this to work:

modelnine at gj-celle ~ $ php
<?php echo 1+'2'; ?>
3
modelnine at gj-celle ~ $

through all kinds of type-casting magic, which isn't explicitly
specified as interfaces on the objects (PHP also has integer and string
objects) themselves.

-- 
--- Heiko.



More information about the Python-list mailing list