Beginnger Question

Chris chris at cmb-enterprises.com
Tue Apr 9 12:55:35 EDT 2002


In article <a8v0mo$j50$1 at newsg1.svr.pol.co.uk>,
 "¨" <simon at piranhaflt.freeserve.co.uk> wrote:

> I'm a newbie myself, so this is probably wrong, but wouldn't this be *even*
> better?
> 
> x = 2
> def wrong(num):
>     x = x+1
>     print x
> 
> wrong(x)
> wrong(x)

The problem with this, in a dynamically typed language like Python, is 
that I could do the following:

>>> x = 2
>>> def wrong():
>>>    global x
>>>    x = x + 1
>>>    print x
>>>
>>>
>>> wrong()
>>> x
3
>>>
>>> x = "hello"
>>> wrong()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in foo
TypeError: cannot concatenate 'str' and 'int' objects
>>>



More information about the Python-list mailing list