user3685952
user3685952

Reputation: 75

How to have the ## C functionality in python

In C, if you have a variable name and you have a variable you would like to access in protobuf

# define get_value(variable, variable_name, store){   \
    store = variable->name_variable_name    \
 }  

Is there a similar functionality in python?

An alternative I can think of is just to do a large if else case

if protobuf_message.type = "LISTENER1":
    message = protobuf_message.LISTENER1
elif protobuf_message.listener2 = "LISTENER2":
    message = protobuf_message.LISTENER2

I would like to do something like this

listener_type = protobuf_message.type
message = protobuf_message.listener_type

Any advice would be appreciated

Upvotes: 0

Views: 39

Answers (1)

chepner
chepner

Reputation: 531205

You are looking for the getattr function.

listener_type = protobuf_message.type
message = getattr(protobuf_message, listener_type)

Upvotes: 2

Related Questions