我尝试通过传递变量来创建一个对象,但它似乎不起作用。
我在下面放了一个简单的例子来说明我想要什么。请帮我处理这个问题。
成功地
temp = catalog.TEST
temp = catalog.PROD
不起作用,它传递字符串“i”而不是列表元素作为属性
lists = ['TEST,'PROD']
for i in lists:
temp = catalog.i
完整代码我正在使用 dremio-client 这个包(https://dremio-client.readthedocs.io/en/latest/readme.html)
import dremio_client as dc
mydremio = dc.init(os.getcwd() + '/.config')
catalog = mydremio.data
# TEST and PROD are the folders that exist in my dremio server
a = catalog.TEST
b = catalog.PROD
# Cannot pass the list element to object "catalog"
list = ["TEST","PROD"]
for i in list
temp = catalog.i
感谢 Pavel 的解决方案,但我有一个更复杂的问题。
list = ["TEST","TEST.DEMO"]
# what's work - directly declare
test = catalog.TEST.DEMO
# works for "TEST" not works for TEST.DEMO
for i in list:
temp = getattr(catalog, i)