V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yangsunrise
V2EX  ›  Python

问一个较基础的问题,关于 Python 语句执行顺序?

  •  
  •   yangsunrise · 2017-02-16 23:46:40 +08:00 · 3345 次点击
    这是一个创建于 2636 天前的主题,其中的信息可能已经有所发展或是发生改变。

    def minmax(test, *args): res = args[0] for arg in args[1:]: if test(arg, res): res = arg return res

    def lessthan(x, y): return x<y def grtrthan(x, y): return x>y

    如上代码的执行顺序,当执行到 if test 时是怎样的? def minmax 和 def lessthan 执行顺序是怎样的呢? 谢谢-__-

    第 1 条附言  ·  2017-02-17 12:56:48 +08:00
    上面复制粘贴的代码,好好的提交后就乱了。。。。
    def minmax(test, *args):
    res = args[0]
    for arg in args[1:]:
    if test(arg, res):
    res = arg
    return res

    def lessthan(x, y): return x<y
    def grtrthan(x, y): teturn x>y

    增加两句调用

    print(minmax(lessthan, 4,5,3,2,1))
    ### 1

    print(minmax(grtrthan, 4,5,3,2,1))
    ### 5

    (重新输入一遍,预览看了下,换行正常但缩进还是被自动删除了。。。。)
    12 条回复    2017-02-17 18:49:13 +08:00
    holajamc
        1
    holajamc  
       2017-02-17 00:10:53 +08:00 via iPhone
    这,楼主的代码缩进有一些惊奇。。。
    grimpil
        2
    grimpil  
       2017-02-17 00:21:51 +08:00
    语法错误,无法执行
    cszeus
        3
    cszeus  
       2017-02-17 02:16:26 +08:00
    if test 执行 test 函数,返回值作为 if 的判断条件。
    声明的话顺序的,哪个在前面,哪个先声明
    fortunezhang
        4
    fortunezhang  
       2017-02-17 08:34:21 +08:00
    if test 是在 for arg 新 args[1:],执行后,第一条执行的。
    def minmax(test,*args):
    res = args[0]
    for arg in args[1:]:
    if test(arg, res):
    res = arg
    return res
    你不格式化,后面很难看懂啊。我用了 3 分钟才看出来。
    fortunezhang
        5
    fortunezhang  
       2017-02-17 08:37:08 +08:00
    我的天,我的代码也没有格式化?求助怎么缩进啊
    jingniao
        6
    jingniao  
       2017-02-17 09:04:26 +08:00
    试试 md 支持?
    ```python
    def minmax(test,*args):
    res = args[0]
    for arg in args[1:]:
    if test(arg, res):
    res = arg
    return res
    def lessthan(x, y):
    return x<y
    def grtrthan(x, y):
    return x>y
    ```
    另外你只是三个函数定义吧?哪来的执行,相互之间也没有调用关系
    lynndotconfig
        7
    lynndotconfig  
       2017-02-17 10:16:31 +08:00
    def minmax(test, *args):
    res = args[0]
    for arg in args[1:]:
    if test(arg, res):
    res = arg
    return res


    def lessthan(x, y):
    return x < y


    def grtrthan(x, y):
    return x > y

    find_less = minmax(lessthan, 5, 6, 7, 8, 9, 4)
    find_greater = minmax(grtrthan, 5, 6, 4, 3, 2, 1)
    print find_less, find_greater

    # ##### Output
    # 4 6
    yangsunrise
        8
    yangsunrise  
    OP
       2017-02-17 12:59:25 +08:00
    @lynndotconfig if test 下的 res = agr 和 return x<y(return x>y)是什么关系?
    yangsunrise
        9
    yangsunrise  
    OP
       2017-02-17 13:05:49 +08:00
    @fortunezhang v2 不知为什么自动删除了代码的缩进换行格式。。。。就是用 python 实现 py 内置函数 min , max 的功能
    lynndotconfig
        10
    lynndotconfig  
       2017-02-17 15:15:28 +08:00   ❤️ 1
    @yangsunrise

    如果 test=lessthan:
    test(arg, res) <==> lessthan(arg, res) <==> (return arg < res) <==>  True or False

    如果 test=grtrthan:
    tese(arg, res) <==> grtrthen(arg, res) <==> ( return arg > res) <==> True or False

    是不明白在 python 中函数名可以作为参数这个问题吗?
    lynndotconfig
        11
    lynndotconfig  
       2017-02-17 15:21:54 +08:00
    @yangsunrise 第一条附言中的的输出应该是 3 和 5 。
    yangsunrise
        12
    yangsunrise  
    OP
       2017-02-17 18:49:13 +08:00
    @lynndotconfig 那就是到 if 是调用 def lessthan ,先执行 return 再执行 res = arg , return 用于 if 条件判断?大概懂了,就是没想到 return x<y 是判断语句。谢谢啦
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1402 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:20 · PVG 01:20 · LAX 10:20 · JFK 13:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.