Namen und Namensräume

In [1]:
import sys
print(sys.version)
3.5.3 |Anaconda 4.4.0 (64-bit)| (default, Mar  6 2017, 11:58:13) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
In [2]:
version = "8.9"
In [3]:
print(version)
8.9
In [4]:
x = "test"
print(x)
test
In [5]:
def f(c):
    x = 8.4
    print(x)
    return c*x

f(2)
8.4
Out[5]:
16.8
In [6]:
print(x)
test
In [7]:
def f(c):
    print(x)
    x = 4.1
    print(x)
    return c*x

f(2)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-7-2543beda3f67> in <module>()
      5     return c*x
      6 
----> 7 f(2)

<ipython-input-7-2543beda3f67> in f(c)
      1 def f(c):
----> 2     print(x)
      3     x = 4.1
      4     print(x)
      5     return c*x

UnboundLocalError: local variable 'x' referenced before assignment
In [8]:
def f(c):
    print(x)
    return c*x

f(2)
test
Out[8]:
'testtest'
In [9]:
def f(c):
    global x
    print(x)
    x = 4.1
    print(x)
    return c*x

print(x)
f(2)
print(x)
test
test
4.1
4.1
In [10]:
x = "test"

def f(c,x):
    print(x)
    x = 4.1
    print(x)
    return (c*x,x)

print(x)
(erg,x) = f(2,x)
print(x)
test
test
4.1
4.1