Python新手关于元组的问题
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是序列解包的情况。
一种更易读的编写方式,也是我个人喜欢的风格,是:
理解前面的示例需要两个位:
(foo,)
。 在大多数情况下,可以省略括号。 特别是,可以在赋值运算符旁边省略它们。This is a case of sequence unpacking.
A more readable way to write the same, and the style I personally favor, is:
There are two bits required to understand the previous example:
(foo,)
. In most contexts, the parenthesis can be ommitted. In particular, they can be omitted next to the assignment operator.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.
逗号用于解压元组,即提取元组的单个项目,并将其绑定到
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.