First of all, your example is wrong: the function should not receive y as a parameter, as "y=2*x" defines a local variable named y. If you want to alter a global variable called y, you need to put "global y" inside your function to override the interpreter assumption that names are local variables.
C:\windows\system32>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> y=5
>>> def double1(x):
... print(y)
... y=x*2
... print(y)
...
>>> print(y)
5
>>> double1(25)
Traceback (most recent call last):
File "", line 1, in
File "", line 2, in double1
UnboundLocalError: local variable 'y' referenced before assignment
>>> def double2(x):
... y=x*2
... print(y)
...
>>> double2(25)
50
>>> print(y)
5
>>> def double3(x):
... global y
... y=x*2
... print(y)
...
>>> double3(13)
26
>>> print(y)
26
Functions and classes are perfectly well behaved objects that you can replace
as a whole, like this:.
C:\windows\system32>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(x):
... return x+1
...
>>> def g(x):
... return x+2
...
>>> print(f(1))
2
>>> print(g(1))
3
>>> f,g = g,f
>>> print(f(1))
3
>>> print(g(1))
2
>>> f=None
>>> print(f(1))
Traceback (most recent call last):
File "", line 1, in
TypeError: 'NoneType' object is not callable
However, classes and functions are for practical purposes global data, and altering them might be a bad idea: conflicts between call sites who want different variations of the function are likely.
Consider more mainstream solutions like using different functions or using additional parameters to get different behaviour.