Passing an attribute name to a function in Python

user46242

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.

jonrsharpe

You have two problems:

  1. 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
  2. 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".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing an attribute name to a function in Python

From Dev

python passing variable name to function

From Dev

Class instance has no attribute 'function_name' for passing object outside class- python

From Dev

Python3:Passing function name as arguement

From Dev

Passing a function as an attribute value

From Dev

Passing a function name to a function

From Dev

Python: Attribute Error When Passing Method as Function Argument

From Java

Passing file name to function

From Dev

Javascript: Passing dynamic data name attribute in Ajax

From Dev

Passing function name as argument in MATLAB

From Dev

Passing a variable name to a function in R

From Dev

passing variable without a name to function

From Dev

Passing an array name to a function, then populating it

From Dev

passing variable without a name to function

From Dev

Passing function name to Func delegate

From Dev

Passing function arguments by Name Scala

From Dev

Python attribute name with a variable in it

From Dev

Passing attribute to custom element in Polymer as a function

From Dev

Python Passing a Function to an Object

From Dev

passing a function as an argument in python

From Dev

Passing Python function with no argument

From Dev

Passing method to a function in Python

From Dev

passing parameters in python function

From Dev

passing a function reference in python

From Dev

Passing Python function with no argument

From Dev

Python Passing a Function to an Object

From Dev

Passing tuple to a Python function

From Dev

Python: Passing function with arguments to a function

From Dev

Python: Passing function with arguments to a function

Related Related

HotTag

Archive