Python新手关于元组的问题

发布于 2024-07-09 07:37:40 字数 1255 浏览 7 评论 0原文

我是 Python 新手,正在使用 cx_Oracle 模块编写一些数据库代码。 在cx_Oracle文档中,他们有一个如下的代码示例:

import sys
import cx_Oracle

connection = cx_Oracle.Connection("user/pw@tns")
cursor = connection.cursor()

try:
    cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
    error, = exc.args
    print >> sys.stderr, "Oracle-Error-Code:", error.code
    print >> sys.stderr, "Oracle-Error-Message:", error.message

我的问题必须处理创建“错误”对象的位置。 “,=”有什么作用? 我尝试搜索 Python 文档,当您搜索运算符时,搜索引擎不能很好地工作。 :-)

我知道 exc.args 是一个单例元组,但我只是不理解“, =”语法。 如果删除逗号,则会收到错误消息“AttributeError: 'tuple' object has no attribute 'code'”。

有人可以指出我的记录在哪里吗? 谢谢!

编辑:

这无需解压元组即可工作:

import sys
import cx_Oracle

connection = cx_Oracle.Connection("user/pw@tns")
cursor = connection.cursor()

try:
    cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
    print >> sys.stderr, "Oracle-Error-Code:", exc.args[0].code
    print >> sys.stderr, "Oracle-Error-Message:", exc.args[0].message

I am new to Python, and I'm working on writing some database code using the cx_Oracle module. In the cx_Oracle documentation they have a code example like this:

import sys
import cx_Oracle

connection = cx_Oracle.Connection("user/pw@tns")
cursor = connection.cursor()

try:
    cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
    error, = exc.args
    print >> sys.stderr, "Oracle-Error-Code:", error.code
    print >> sys.stderr, "Oracle-Error-Message:", error.message

My question has to do with where the "error" object is created. What does the ", =" do? I tried searching Python documentation, and search engines don't work very well when you're searching for operators. :-)

I know that the exc.args is a singleton tuple, but I just don't understand the ", =" syntax. If I remove the comma, I get the error message, "AttributeError: 'tuple' object has no attribute 'code'".

Can someone point me to where this is documented? Thanks!

EDIT:

This works without having to unpack the tuple:

import sys
import cx_Oracle

connection = cx_Oracle.Connection("user/pw@tns")
cursor = connection.cursor()

try:
    cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
    print >> sys.stderr, "Oracle-Error-Code:", exc.args[0].code
    print >> sys.stderr, "Oracle-Error-Message:", exc.args[0].message

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

未蓝澄海的烟 2024-07-16 07:37:40
error, = exc.args

这是序列解包的情况。

一种更易读的编写方式,也是我个人喜欢的风格,是:

[error] = exc.args

理解前面的示例需要两个位:

  1. 当赋值的左侧是名称的递归序列时,右侧的值side 必须是长度相同的序列,并且将 RHS 值的每一项分配给 LHS 中对应的名称。
  2. Python 中的单项元组写作 (foo,)。 在大多数情况下,可以省略括号。 特别是,可以在赋值运算符旁边省略它们。
error, = exc.args

This is a case of sequence unpacking.

A more readable way to write the same, and the style I personally favor, is:

[error] = exc.args

There are two bits required to understand the previous example:

  1. When the left hand side of an assignment is a recursive sequence of names, the value of the right hand side must be a sequence with the same length, and each item of the RHS value is assigned to the corresponding name in the LHS.
  2. A one-item tuple in python is written (foo,). In most contexts, the parenthesis can be ommitted. In particular, they can be omitted next to the assignment operator.
素衣风尘叹 2024-07-16 07:37:40

http://www.python.org/doc/2.5。 2/tut/node7.html

在 5.3 节中查找“序列解包”。

http://www.python.org/doc/2.5.2/tut/node7.html

Look for "sequence unpacking" in section 5.3.

鹿港巷口少年归 2024-07-16 07:37:40

逗号用于解压元组,即提取元组的单个项目,并将其绑定到error。 如果没有逗号,您将绑定元组本身,而不是其内容。

The comma serves to unpack the tuple, i.e. it extracts the single item of the tuple, and binds it to error. Without the comma, you would bind the tuple itself, rather than its content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文