Vishal
Vishal

Reputation: 20627

Any utility function in python which returns value when passed object and attribute to it?

Like we have in Java Beans util where you pass object and the property name it gives you the value do we have anything similar in python:

def attr(obj, attr)
    return obj.attr

Upvotes: 0

Views: 99

Answers (1)

ezod
ezod

Reputation: 7421

You can use getattr for this purpose. From the built-in function documentation:

For example, getattr(x, 'foobar') is equivalent to x.foobar.

Upvotes: 6

Related Questions