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

新手求教怎么修改函数里的判断

  •  
  •   ThunderEX · 2012-08-15 08:53:25 +08:00 · 2906 次点击
    这是一个创建于 4266 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大家早上好,问题是这样的:
    修改前是三个函数:
    funA()
    funB()
    funC()
    其中
    def funC():
    ...#若干嵌套
    if X:
    cry_out
    if Y:
    do_sth
    现在想要改成:funA、funB、funC一路跑下来,第一次遇到if X为True的时候不执行cry_out,而是倒回去把funA、funB、funC再跑一遍,再次遇到if X为True再cry_out,当然if X为False的行为不变。
    我有很多笨办法比如设flag之类但自己感觉不好,所以想求教一下各位高手怎么改比较漂亮?
    3 条回复    1970-01-01 08:00:00 +08:00
    lilydjwg
        1
    lilydjwg  
       2012-08-15 09:08:39 +08:00   ❤️ 1
    这样?
    rerun = False
    while True:
    funA()
    funB()
    if not funC(rerun):
    break

    def funC(rerun):
    #...
    if X and not rerun:
    return True
    if X:
    cry_out
    lilydjwg
        2
    lilydjwg  
       2012-08-15 09:09:37 +08:00
    囧,忘记置位 rerun 了……
    另外,缩进没了怎么办?
    ThunderEX
        3
    ThunderEX  
    OP
       2012-08-15 10:44:09 +08:00
    @lilydjwg 我差不多也是这样想的,就是觉得既改动definition又改动reference不太雅观。还是谢谢~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2637 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:29 · PVG 19:29 · LAX 04:29 · JFK 07:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.