python3 和 python2 系列的用法 - V2EX
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 4664 次点击
    这是一个创建于 3988 天前的主题,其中的信息可能已经有所发展或是发生改变。
    '''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     Solana     2441 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 11:42 PVG 19:42 LAX 04:42 JFK 07:42
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86