You have two problems:
- If you try to call
foo(apple, color), you get a NameError because color isn't defined in the scope from which you're calling foo; and
- If you try to call
foo(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".