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
fingerstyle
V2EX  ›  Python

问个菜鸟问题,关于函数

  •  
  •   fingerstyle · 2017-03-08 19:39:05 +08:00 · 1962 次点击
    这是一个创建于 2603 天前的主题,其中的信息可能已经有所发展或是发生改变。
    shell 的函数中如果没写 return ,在调用的时候相当于调取了函数中的所有内容,但 python 中如果不写 return 只会返回 None ,请问在 python 中怎样实现 shell 那样的函数调用,调取一个函数中的全部内容?
    7 条回复    2017-03-08 20:17:32 +08:00
    clino
        1
    clino  
       2017-03-08 19:51:27 +08:00 via Android
    尴尬了,看不懂。。。
    jingniao
        2
    jingniao  
       2017-03-08 19:52:10 +08:00 via Android
    同没看明白,说的啥意思
    fingerstyle
        3
    fingerstyle  
    OP
       2017-03-08 20:10:13 +08:00
    @clino @jingniao

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

    example

    echo $a

    运行后输出 1

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

    如果是的话,解决办法是在赋值 a 的上一行声明一句
    ```
    global a
    ```
    imn1
        6
    imn1  
       2017-03-08 20:15:25 +08:00
    全局变量?
    fingerstyle
        7
    fingerstyle  
    OP
       2017-03-08 20:17:32 +08:00
    ok 是全局变量 明白了 感谢大家
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1051 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:31 · PVG 06:31 · LAX 15:31 · JFK 18:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.