How do I pass the name of an object's attribute to a function? For example, I tried:
def foo(object, attribute):
output = str(object.attribute)
print(output)
class Fruit:
def __init__(self, color):
self.color = color
apple = Fruit("red")
foo(apple, color)
but the above doesn't work because Python thinks that, in foo(apple, color)
, the color
refers to an unitialized variable.
You have two problems:
foo(apple, color)
, you get a NameError
because color
isn't defined in the scope from which you're calling foo
; andfoo(apple, 'color')
you get an AttributeError
because Fruit.attribute
doesn't exist - you are not, at that point, actually using the attribute
argument to foo
.I think what you want to do is access an attribute from a string of the attribute's name, for which you can use getattr
:
>>> def foo(obj, attr):
output = str(getattr(obj, attr))
print(output)
>>> foo(apple, 'color')
red
Note that you shouldn't use object
as a variable name, as it shadows the built-in type.
As a demonstration of point #2:
>>> class Test:
pass
>>> def demo(obj, attr):
print(attr)
print(obj.attr)
>>> t = Test()
>>> t.attr = "foo"
>>> t.bar = "baz"
>>> demo(t, "bar")
bar # the value of the argument 'attr'
foo # the value of the 'Test' instance's 'attr' attribute
Note that neither value is "baz"
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments