【问题标题】:Directly Reference Python's Standard Library直接引用 Python 的标准库
【发布时间】:2014-08-25 01:55:09
【问题描述】:

原来PyQt重新定义了一个函数hex(),不幸的是导致python标准库hex()不可用。我正在开发一个大型软件项目,它是使用 *imports 设置的:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

...等

我需要标准的pythonhex()函数,有什么方法可以参考吗?我在考虑stdlib.hex() 或类似的东西?

目前我丑陋的解决方法是:

pyHex = hex
from PyQt4.QtCore import *
from PyQt4.QtGui import *
hex = pyHex

我真的不想这样做。

谢谢。

【问题讨论】:

  • 我认为,这是 * 导入被称为不良做法的原因之一
  • 我同意——不幸的是,我不是唯一一个在这个代码库上工作的人,而且 * 导入是在我看到代码之前很久就完成的。所以现在我需要一个解决方法。

标签: python python-2.7 pyqt4 standard-library


【解决方案1】:
from __builtin__ import hex

使用__builtin__ module。

【讨论】:

  • 请注意,在 Python 3 中,__builtin__ 已重命名为 builtins。
  • 如果 OP 需要同时使用 QT hex 和 Python hex,那么他们可能想要这样做:from _builtin_ import hex as _hex
猜你喜欢
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2018-04-03
  • 2014-03-09
相关资源
最近更新 更多