not safe at all

Christian Tanzer tanzer at swing.co.at
Fri Jul 13 11:51:46 EDT 2001


Dennis Roark <denro at earthlink.net> wrote:

> For amusement, run this little script which demonstrates a
> near ultimate in the lack of type safety in the language.
> (Perhaps there is a bit of type safety in that you can't do
> this:  4 + "one") But look at what you can do in the
> following script:
> 
> x = 3
> x = x + 2
> print "x =", x
> x = "now I'm a string"
> print x
> x = [ 5, x ]
> print "and now a list:", x
> 
> If you don't believe it, run it.  Should any language allow
> x to be such a chameleon, to where it can even be a
> different type on two sides of an assignment?

You're out of your depth here.

Lets run your code and then some and look what's unsafe in there:

    >>> x = 3
    >>> x = x + 2
    >>> print "x =", x
    x = 5
    >>> x.sort()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    AttributeError: 'int' object has no attribute 'sort'
    >>> x.upper()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    AttributeError: 'int' object has no attribute 'upper'

Currently, x is bound to an integer object and thus doesn't accept
calls to string or list methods. It complains loudly about trying to
force them on it.

    >>> x = "now I'm a string"
    >>> print x
    now I'm a string
    >>> x.sort()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    AttributeError: sort
    >>> x.upper()
    "NOW I'M A STRING"
    >>> x = x + 2
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: cannot add type "int" to string

After the rebinding, x refers to a string object and accepts calls to
string methods, but complains just as loud as the former int-binding
when called with int or list methods.

    >>> x = [ 5, x ]
    >>> print "and now a list:", x
    and now a list: [5, "now I'm a string"]
    >>> x.sort()
    >>> x.upper()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    AttributeError: upper
    >>> x = x + 2
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: can only concatenate list (not "int") to list

And for a binding to a list object, just the same applies -- it will
gladly perform list methods but refuses to do any string or int stuff.

In Python, objects are typed, not references. If you are used to 
languages where variables are (more or less) strongly typed but
objects aren't, you'll have to shed some old habits and acquire a new
point of view to appreciate Python's object model.

I'd recommend you go for it. Learning Python's object model will open
a new world for you and probably make you a better programmer in your
current language(s) of choice, too. A good start for this might be

    http://effbot.org/guides/python-objects.htm

Cheers,
Christian

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list