Strong/weak typing

Carl Banks pavlovevidence at gmail.com
Fri Aug 1 14:23:31 EDT 2008


On Aug 1, 11:31 am, MartinRineh... at gmail.com wrote:
> I'm writing Python as if it were strongly typed, never recycling a
> name to hold a type other than the original type.
>
> Is this good software engineering practice, or am I missing something
> Pythonic?

I don't think you should go about gratuitously rebinding names to
objects of different types just for the sake of being more Pythonic,
no.

The strength of dynamic typing (Pythonistas prefer the terms dynamic
vs static for what you describe, and use weak vs stong for something
else) lies mostly in the freedom it gives you.


It means you can write a function like this and call it on any object
that has an output method:

def function(x):
    x.output()

It means you can have big list of objects of different types and
iterate through the items  like this, as long they all have that
output method:

for x in some_bug_list:
    x.output()

You can arrange for this kind of thing to happen in statically-typed
languages as well, but it's a lot more effort (defining interfaces or
subclasses).

IMO, organizing code to take advantage of this can result in much
simpler logic with much less code duplication.  But even if you do
that, most of the variables in a program are going to spend their
whole time being bound to a single time.


Carl Banks



More information about the Python-list mailing list