推荐学习书目
› 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 · Nov 23, 2014 · 5292 views
    This topic created in 4332 days ago, the information mentioned may be changed or developed.
    '''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 replies  •  2014-11-23 19:00:10 +08:00
    ryd994
        1
    ryd994  
       Nov 23, 2014 via Android
    return()吧
    ryd994
        2
    ryd994  
       Nov 23, 2014 via Android
    我错了,没事了
    p8p8
        3
    p8p8  
    OP
       Nov 23, 2014
    @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
       Nov 23, 2014
    @ryd994 哈哈哈,没事,大家一起学习进步!
    iceout
        5
    iceout  
       Nov 23, 2014
    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
       Nov 23, 2014
    @iceout 嗯,然后呢?2系列中应该怎么用呢?
    iceout
        7
    iceout  
       Nov 23, 2014
    @p8p8 第一句。。raise exception

    文档中的例子:

    @gen.coroutine
    def fetch_json(url):
    response = yield AsyncHTTPClient().fetch(url)
    raise gen.Return(json_decode(response.body))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   753 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 20:59 · PVG 04:59 · LAX 13:59 · JFK 16:59
    ♥ Do have faith in what you're doing.