推荐学习书目
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
fingerstyle
V2EX  ›  Python

问个菜鸟问题,关于函数

  •  
  •   fingerstyle · Mar 8, 2017 · 2854 views
    This topic created in 3369 days ago, the information mentioned may be changed or developed.
    shell 的函数中如果没写 return ,在调用的时候相当于调取了函数中的所有内容,但 python 中如果不写 return 只会返回 None ,请问在 python 中怎样实现 shell 那样的函数调用,调取一个函数中的全部内容?
    7 replies    2017-03-08 20:17:32 +08:00
    clino
        1
    clino  
       Mar 8, 2017 via Android
    尴尬了,看不懂。。。
    jingniao
        2
    jingniao  
       Mar 8, 2017 via Android
    同没看明白,说的啥意思
    fingerstyle
        3
    fingerstyle  
    OP
       Mar 8, 2017
    @clino @jingniao

    比如在 shell 里:
    example(){
    a=1
    }

    example

    echo $a

    运行后输出 1

    在 python 里如果不写 return ,返回的值就是 None ,
    fingerstyle
        4
    fingerstyle  
    OP
       Mar 8, 2017
    python 里可以做到 shell 这种形式的函数调用吗 无需写明 return 具体返回值 调用函数中的所有语句
    kindjeff
        5
    kindjeff  
       Mar 8, 2017
    虽然很困难,但是我觉得我肯定猜中了你的意思。我猜你的问题应该是在 python 函数内部给一个临时变量赋值,但是它在函数的外层没法表达出来,除非 return 它,对吗?

    如果是的话,解决办法是在赋值 a 的上一行声明一句
    ```
    global a
    ```
    imn1
        6
    imn1  
       Mar 8, 2017
    全局变量?
    fingerstyle
        7
    fingerstyle  
    OP
       Mar 8, 2017
    ok 是全局变量 明白了 感谢大家
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3137 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 03:13 · PVG 11:13 · LAX 20:13 · JFK 23:13
    ♥ Do have faith in what you're doing.