Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 确保在类定义中删除不需要的名称_Python 3.x_Compatibility_Python 2.x - Fatal编程技术网

Python 3.x 确保在类定义中删除不需要的名称

Python 3.x 确保在类定义中删除不需要的名称,python-3.x,compatibility,python-2.x,Python 3.x,Compatibility,Python 2.x,我如何确保附带的名称不会出现在类定义中,代码可以同时在Python2和Python3上工作 使用以下类定义,仅用于列表理解的附带名称'foo'和'bar',保留在Parrot命名空间中: \uuuuu元类\uuuuu=type 班鹦鹉: “羽毛美丽的鹦鹉。” 羽毛=[ (foo,bar)表示羽毛中的(foo,bar) 如果条==“漂亮”] 断言hasattr(鹦鹉,羽毛)#← 好的,有通缉犯的名字吗 断言不是hasattr(鹦鹉学舌,foo')#← 失败,有一个不需要的名称 断言不是hasatt

我如何确保附带的名称不会出现在类定义中,代码可以同时在Python2和Python3上工作

使用以下类定义,仅用于列表理解的附带名称
'foo'
'bar'
,保留在
Parrot
命名空间中:

\uuuuu元类\uuuuu=type
班鹦鹉:
“羽毛美丽的鹦鹉。”
羽毛=[
(foo,bar)表示羽毛中的(foo,bar)
如果条==“漂亮”]
断言hasattr(鹦鹉,羽毛)#← 好的,有通缉犯的名字吗
断言不是hasattr(鹦鹉学舌,foo')#← 失败,有一个不需要的名称
断言不是hasattr(鹦鹉学舌,'bar')#← 失败,有一个不需要的名称
因此,我可以在使用这些名称后删除它们:

\uuuuu元类\uuuuu=type
班鹦鹉:
“羽毛美丽的鹦鹉。”
羽毛=[
(foo,bar)表示羽毛中的(foo,bar)
如果条==“漂亮”]
德尔福酒吧
断言hasattr(鹦鹉,羽毛)#← 好的,有通缉犯的名字吗
断言不是hasattr(鹦鹉学舌,foo')#← 好的,没有多余的名字
断言不是hasattr(鹦鹉学舌,'bar')#← 好的,没有多余的名字
但这在Python 3上失败了,因为名称不会从列表中持久化:

\uuuuu元类\uuuuu=type
班鹦鹉:
“羽毛美丽的鹦鹉。”
羽毛=[
(foo,bar)表示羽毛中的(foo,bar)
如果条==“漂亮”]
德尔福酒吧← 失败,“名称错误:未定义名称“foo”

如何使用列表理解编写类定义,而不将附带的名称保留在可以在Python 2和Python 3上正确运行的代码中?

您可以在类范围内捕获
namererror
,以实现此功能:

class Parrot:
    """ A parrot with beautiful plumage. """
    plumage = [
            (foo, bar) for (foo, bar) in feathers.items()
            if bar == "beautiful"]
    try:
        del foo, bar
    except NameError:
        pass
您可能需要对此进行一些调整,以确保尝试/捕获每个单独的变量名

不相关,但你可以通过这种方式做一些令人惊讶的事情:

>>> class A(object):
...     import collections
...
>>> A().collections.defaultdict(list)
defaultdict(<type 'list'>, {})
>>A类(对象):
...     导入集合
...
>>>A().collections.defaultdict(列表)
defaultdict(,{})

对于列表理解的特定情况,它将泄漏Python 2中的名称,而不会泄漏Python 3中的名称。这是

另一方面,在Python2或Python3中都不会泄漏名称。因此,可以从生成器构造列表:

\uuuuu元类\uuuuu=type
班鹦鹉:
“羽毛美丽的鹦鹉。”
羽毛=列表(
(foo,bar)表示羽毛中的(foo,bar)
如果条==“美丽”)
断言hasattr(鹦鹉,羽毛)#← 好的,有通缉犯的名字吗
断言不是hasattr(鹦鹉学舌,foo')#← 好的,没有多余的名字
断言不是hasattr(鹦鹉学舌,'bar')#← 好的,没有多余的名字

这将导致类名称空间中没有“泄漏”的名称,无论是在Python2还是Python3中。

这通常是一个很好的解决方案,+1。不过,具体的问题有一个更好的答案。@bignose:谢谢。我同意你的解决方案更好。