Python 基于用户输入访问类变量

Python 基于用户输入访问类变量,python,class,jira,Python,Class,Jira,我希望能够基于用户输入访问类变量,因为我从JiraAPI调用中获得了一个类。比如说 test = my_jira.issue("ISSUE-1799") test.fields.summary = "Test issue" # sets summary field # user can enter anything here and I can access any variable from test.fields. random = "su

我希望能够基于用户输入访问类变量,因为我从JiraAPI调用中获得了一个类。比如说

test = my_jira.issue("ISSUE-1799")
test.fields.summary = "Test issue" # sets summary field

# user can enter anything here and I can access any variable from test.fields.
random = "summary"
print(test.fields.(random)) # prints "Test issue"

这可能吗?
test.field
中有一组类变量,我希望能够根据用户输入的内容访问任何一个类变量。对不起,如果这是错误的。我真的不知道如何描述这一点。

是的,这是可能的,您可以像这样使用内置功能:

print(getattr(test.fields, random))

是的,这是可能的,您可以像这样使用内置功能:

print(getattr(test.fields, random))

您可以使用
getattr
从类中获取属性。第三个参数是默认参数,如果属性不存在,将返回该参数。考虑到您希望允许用户输入他们想要访问的属性,您一定要使用第三个参数,并准备在属性不存在时向用户传递消息。否则,错误将导致错误破坏脚本

如果
测试字段
a
dict

#example
attrName = input("Type the attribute name you would like to access: ")
attr = getattr(test.fields, attrName, None)

if attr is None:
    print(f'Attribute {attrName} does not exist')
else:
    print(f'{attrName} = {attr}')
attrList = [*test.fields]  #list of keys
attrName = input("Type the attribute name you would like to access: ")

if attrName in attrList:
    attr = test.fields[attrName]
    print(f'{attrName} = {attr}')
else:
    print(f'Attribute {attrName} does not exist')
如果
test.fields
是一个
dict

#example
attrName = input("Type the attribute name you would like to access: ")
attr = getattr(test.fields, attrName, None)

if attr is None:
    print(f'Attribute {attrName} does not exist')
else:
    print(f'{attrName} = {attr}')
attrList = [*test.fields]  #list of keys
attrName = input("Type the attribute name you would like to access: ")

if attrName in attrList:
    attr = test.fields[attrName]
    print(f'{attrName} = {attr}')
else:
    print(f'Attribute {attrName} does not exist')

您应该注意,
random
是一个python模块。使用通用模块名作为变量名不是一种好的做法。如果您碰巧为连接到此脚本的任何内容导入了
random
,则可能会出现问题。

您可以使用
getattr
从类中获取属性。第三个参数是默认参数,如果属性不存在,将返回该参数。考虑到您希望允许用户输入他们想要访问的属性,您一定要使用第三个参数,并准备在属性不存在时向用户传递消息。否则,错误将导致错误破坏脚本

如果
测试字段
a
dict

#example
attrName = input("Type the attribute name you would like to access: ")
attr = getattr(test.fields, attrName, None)

if attr is None:
    print(f'Attribute {attrName} does not exist')
else:
    print(f'{attrName} = {attr}')
attrList = [*test.fields]  #list of keys
attrName = input("Type the attribute name you would like to access: ")

if attrName in attrList:
    attr = test.fields[attrName]
    print(f'{attrName} = {attr}')
else:
    print(f'Attribute {attrName} does not exist')
如果
test.fields
是一个
dict

#example
attrName = input("Type the attribute name you would like to access: ")
attr = getattr(test.fields, attrName, None)

if attr is None:
    print(f'Attribute {attrName} does not exist')
else:
    print(f'{attrName} = {attr}')
attrList = [*test.fields]  #list of keys
attrName = input("Type the attribute name you would like to access: ")

if attrName in attrList:
    attr = test.fields[attrName]
    print(f'{attrName} = {attr}')
else:
    print(f'Attribute {attrName} does not exist')

您应该注意,
random
是一个python模块。使用通用模块名作为变量名不是一种好的做法。如果您碰巧为连接到此脚本的任何内容导入了
random
,您可能会遇到问题。

这是一个糟糕的答案。它没有处理
getattr
的第三个参数,并且懒惰地转储所使用的糟糕的编码实践。它还假设
test.fields
不是
dict
。这是一个客观的答案,它回答了问题,如果他需要更多信息,它会链接到文档。代码片段显然是一种表示,而不是实际的应用程序
test.fields
是来自JIRA SDK的JIRA对象,OP提到他得到了“来自JIRA API调用的类”。这是一个糟糕的答案。它没有处理
getattr
的第三个参数,并且懒惰地转储所使用的糟糕的编码实践。它还假设
test.fields
不是
dict
。这是一个客观的答案,它回答了问题,如果他需要更多信息,它会链接到文档。代码片段显然是一种表示,而不是实际的应用程序
test.fields
是来自JIRA SDK的JIRA对象,OP提到他获得了“来自JIRA API调用的类”。