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

python3 和 python2 系列的用法

  •  
  •   p8p8 · 2014-11-23 14:02:06 +08:00 · 4034 次点击
    这是一个创建于 3413 天前的主题,其中的信息可能已经有所发展或是发生改变。
    '''python3 下面这样写没有问题'''

    db = momoko.Pool(dsn='dbname=%s user=%s' % (config.db.database, config.db.user), size=2)

    @tornado.gen.coroutine
    def execute(self, query, *args):
    result = yield momoko.Op(self.db.execute, query, args, cursor_factory=psycopg2.extras.DictCursor)
    return result


    python2 下面会提示 python version < 3.3 do not allow 'return' .....

    所以我把


    @tornado.gen.coroutine
    def execute(self, query, *args):
    result = yield momoko.Op(self.db.execute, query, args, cursor_factory=psycopg2.extras.DictCursor)
    return result


    return result 直接去掉,写了一个 self.finish()


    然后另外一个地方直接这样调用

    @gen.coroutine
    def get(self):
    query = 'INSERT INTO public.user (name, password) VALUES (%s, %s);'
    try:
    yield self.execute(query,'sss','333')
    except (psycopg2.Warning, psycopg2.Error) as error:
    self.write(str(error))
    self.finish()


    这样会有问题么??
    7 条回复    2014-11-23 19:00:10 +08:00
    ryd994
        1
    ryd994  
       2014-11-23 14:20:39 +08:00 via Android
    return()吧
    ryd994
        2
    ryd994  
       2014-11-23 14:22:13 +08:00 via Android
    我错了,没事了
    p8p8
        3
    p8p8  
    OP
       2014-11-23 14:22:39 +08:00
    @ryd994 return()吧 ??

    result = yield momoko.Op(self.db.execute, query, args, cursor_factory=psycopg2.extras.DictCursor)
    return result

    这里改成

    result = yield momoko.Op(self.db.execute, query, args, cursor_factory=psycopg2.extras.DictCursor)
    return()

    这样吗?

    我怎么感觉你的回答好虚啊。。。不过还是谢谢你。
    p8p8
        4
    p8p8  
    OP
       2014-11-23 14:23:06 +08:00
    @ryd994 哈哈哈,没事,大家一起学习进步!
    iceout
        5
    iceout  
       2014-11-23 14:28:27 +08:00
    http://tornado.readthedocs.org/en/branch3.2/gen.html#decorators
    文档中写了。。。

    Coroutines may “return” by raising the special exception Return(value).In Python 3.3+, it is also possible for the function to simply use the return value statement (prior to Python 3.3 generators were not allowed to also return values).
    p8p8
        6
    p8p8  
    OP
       2014-11-23 14:52:17 +08:00
    @iceout 嗯,然后呢?2系列中应该怎么用呢?
    iceout
        7
    iceout  
       2014-11-23 19:00:10 +08:00
    @p8p8 第一句。。raise exception

    文档中的例子:

    @gen.coroutine
    def fetch_json(url):
    response = yield AsyncHTTPClient().fetch(url)
    raise gen.Return(json_decode(response.body))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5898 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 02:20 · PVG 10:20 · LAX 19:20 · JFK 22:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.