Python 对象属性能否作为函数参数传递

Python 对象属性能否作为函数参数传递,python,object,Python,Object,在Python中,我很难将对象属性作为函数参数传递 def selector_string(antique, field, variable): if antique.field == variable: return antique 如果传入相关变量,上述函数将不起作用 selector_string(item, 'country', 'SINGAPORE') >>> Antique instance has no attribute 'fi

在Python中,我很难将对象属性作为函数参数传递

def selector_string(antique, field, variable):

     if antique.field == variable:

        return antique
如果传入相关变量,上述函数将不起作用

selector_string(item, 'country', 'SINGAPORE')

>>> Antique instance has no attribute 'field'
我哪里出错了?谢谢。

您应该像这样使用

if getattr(antique, field) == variable:
if getattr(antique, field, None) == variable:
if hasattr(antique, field):
    if getattr(antique, field) == variable:
        ...
        ...
else:
    print "Attribute '{}' not found in antique".format(field)
引用文件

返回对象的命名属性的值。名称必须是 一串如果字符串是对象属性之一的名称, 结果是该属性的值。例如,getattr(x, “foobar”)相当于x.foobar。如果命名属性不存在 存在,如果提供,则返回默认值,否则返回AttributeError 提高

如果属性不在
仿古
中,并且希望提供默认值,则可以这样做

if getattr(antique, field) == variable:
if getattr(antique, field, None) == variable:
if hasattr(antique, field):
    if getattr(antique, field) == variable:
        ...
        ...
else:
    print "Attribute '{}' not found in antique".format(field)
这里
None
是返回的默认值,如果
字段的值不在

如果您想知道该属性是否真的存在于对象上,可以这样使用

if getattr(antique, field) == variable:
if getattr(antique, field, None) == variable:
if hasattr(antique, field):
    if getattr(antique, field) == variable:
        ...
        ...
else:
    print "Attribute '{}' not found in antique".format(field)
你应该像这样使用

if getattr(antique, field) == variable:
if getattr(antique, field, None) == variable:
if hasattr(antique, field):
    if getattr(antique, field) == variable:
        ...
        ...
else:
    print "Attribute '{}' not found in antique".format(field)
引用文件

返回对象的命名属性的值。名称必须是 一串如果字符串是对象属性之一的名称, 结果是该属性的值。例如,getattr(x, “foobar”)相当于x.foobar。如果命名属性不存在 存在,如果提供,则返回默认值,否则返回AttributeError 提高

如果属性不在
仿古
中,并且希望提供默认值,则可以这样做

if getattr(antique, field) == variable:
if getattr(antique, field, None) == variable:
if hasattr(antique, field):
    if getattr(antique, field) == variable:
        ...
        ...
else:
    print "Attribute '{}' not found in antique".format(field)
这里
None
是返回的默认值,如果
字段的值不在

如果您想知道该属性是否真的存在于对象上,可以这样使用

if getattr(antique, field) == variable:
if getattr(antique, field, None) == variable:
if hasattr(antique, field):
    if getattr(antique, field) == variable:
        ...
        ...
else:
    print "Attribute '{}' not found in antique".format(field)

这确实是可能的,但您不能只说
仿古.field
,因为程序认为您正在查找名为“field”的属性,而不是查找名称存储在
field
中的属性。您可以通过使用
getattr
函数来解决此问题,该函数接受包含字段名称的字符串,并返回具有该名称的字段值:

def selector_string(antique, field, variable):

   if antique.getattr(field) == variable:
       return antique

这确实是可能的,但您不能只说
仿古.field
,因为程序认为您正在查找名为“field”的属性,而不是查找名称存储在
field
中的属性。您可以通过使用
getattr
函数来解决此问题,该函数接受包含字段名称的字符串,并返回具有该名称的字段值:

def selector_string(antique, field, variable):

   if antique.getattr(field) == variable:
       return antique

缩进,可以肯定:pIt根本不是这样工作的。我的建议是:不要试图变得“聪明”。如果你的意思是
item.country
,那就写下它吧。缩进,当然:pIt根本不是这样工作的。我的建议是:不要试图变得“聪明”。如果您的意思是
item.country
,只需将其写入
try,除非使用
或使用默认值将防止
AttributeError
@RedBaron使用默认值更新:)但是,请注意,大多数情况下,当您认为要使用
getattr
时,您真正应该使用的是dict。将其包装在
尝试中,除了
或使用默认值将防止
AttributeError
@RedBaron使用默认值更新:)但是,请注意,大多数时候,当您认为要使用
getattr
时,您真正应该使用的是dict。