姓名:刘浩然。 2020年大目标Python

day15 Python风湿理论之函数即变量

eg1、定义foo门牌号,调用foo函数,打印,再找bar门牌号,找不到,报错

def foo():
    print('from foo')
    bar()

foo()

结果:报错
from foo
Traceback (most recent call last):
  File "/opt/zifuchuangeshihua.py", line 6, in <module>
    foo()
  File "/opt/zifuchuangeshihua.py", line 4, in foo
    bar()
NameError: name 'bar' is not defined

 eg2、

def bar():
    print('from bar')
def foo():
    print('from foo')
    bar()

foo()

结果:
from foo
from bar

 eg3、

def foo():
    print('from foo')
    bar()

def bar():
    print('from bar')
foo()

结果:
from foo
from bar

 eg4、定义foo门牌号,调用foo函数,打印,foo里面有个bar的门牌号,找不到bar门牌号报错

def foo():
    print('from foo')
    bar()

foo()

def bar():
    print('from bar')

结果:报错
Traceback (most recent call last):
  File "/opt/zifuchuangeshihua.py", line 5, in <module>
    foo()
  File "/opt/zifuchuangeshihua.py", line 3, in foo
    bar()
NameError: name 'bar' is not defined
from foo

 eg5、

name='海风'                    #1
def huangwei():               #2      
    name = "黄伟"              #4.1
    print(name)                  #4.2
    def liuyang():                #4.3
        name = "刘洋"           #4.4.1
        print(name)               #4.4.2
        def nulige():              #4.4.3
            name = '炉指花'      #4.4.4.1
            print(name)           # 4.4.4.2 
        print(name)               #打印同级的刘洋
        nulige()                     #4.4.4
    liuyang()                       #4.4
    print(name)                   #4.5打印同级的黄伟    
    
print(name)                       #3
huangwei()                        #4
print(name)                       #5

 

posted @ 2019-02-14 11:17  pluto2charon  阅读(179)  评论(0编辑  收藏  举报