2

Suppose I have a model object.

print (dir(table))
['...', 'col1', 'col2', '...']

# this will output column 1 value
print (table.col1)

I would like to do it dynamically, for example:

col = 'col1'
table.col

Thx

2 Answers 2

3

You want to use getattr when doing dynamic attribute retrieval in python:

col = 'col1'
getattr(table, col)
Sign up to request clarification or add additional context in comments.

Comments

2

To get attribute value by name use getattr

getattr(table, 'col1')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.