Python 从字典填充名称空间?

Python 从字典填充名称空间?,python,dictionary,namespaces,Python,Dictionary,Namespaces,为了清理一段代码,我尝试了以下方法: class ClassDirection(): def __init__(self): pass def downward(self, x): print (x) def upward(self, x): print (X +1) def sideways(self, x): print (x // 2) directions = [] mustard =

为了清理一段代码,我尝试了以下方法:

class ClassDirection():
    def __init__(self):
        pass

    def downward(self, x):
        print (x)

    def upward(self, x):
        print (X +1)

    def sideways(self, x):
        print (x // 2)

directions = []
mustard = ClassDirection()
dicty = {downward:5, upward:7, sideways:9}
for a,b in dicty.items():
    direction = mustard.a(b)
    directions.append(direction)
由于python将单词“downlown”理解为一个未定义的名称,因此它当然不会运行,并给出错误:

NameError: name 'downward' is not defined
关于这一点,我有两个问题。A) 有没有一种方法可以将未定义的“名称”存储在字典中,而不用将其存储为字符串,然后用某种疯狂的破解方法重新格式化?B) 甚至可以像这样“注入”命名空间的一部分吗

dicty = {mustard.downward: 5, mustard.upward: 7, mustard.sideways: 9}
for a, b in dicty.items():
    direction = a(b)
或:

此外,口述在这里并没有真正的帮助,意味着你无法控制秩序。而是:

dicty = [('downward', 5), ('upward', 7), ('sideways', 9)]
for a, b in dicty:  # no .items()

当然,只需存储字符串并使用
getattr()
。或者存储绑定的方法<代码>芥末。向下是一个可以放在字典里的对象,以后再打电话。谢谢!getattr是我一直在寻找的东西,通过查看它的示例,我学到了很多东西。顺便说一句,我认识到我问题中的潜在机制可能是重复的,但找到它的途径可能不是。事实上,在问我的问题之前,我花了很多时间在这里和谷歌上搜索。问题是“我如何描述我在寻找什么”“如何在Python中动态访问类属性?”这根本不是我想问的问题。谢谢!getattr是我一直在寻找的东西,通过查看它的示例,我学到了很多东西。就字典而言:出于某种原因,我觉得这是一种更直接的方式来访问当时的值,因为我不需要跟踪顺序。但进一步考虑,一份简单的清单可能更好。
dicty = [('downward', 5), ('upward', 7), ('sideways', 9)]
for a, b in dicty:  # no .items()