Dot notation problem...

I have a list of strings that I’m looping through, and each one is identical to the name of an attribute of an object, so I need to somehow use the string as the attribute name, to find out whether the value of the attribute is True or False…

For example (a very silly example):


scene = context.scene
strings_list = [ 'list', 'of', 'strings']  # Actual list is quite a bit longer

# Now, each Python object in 'some_list' has BoolProperty attributes named 'list', 'of', and 'strings'

for item in some_list:
    attr_list = [ string for string in strings_list if eval( "item." + string)]
    .....

That last line is the one that gives errors. It works just fine in certain contexts (like in the Python console), but it doesn’t work when it’s being run from an operator. It throws a


NameError: name 'item' is not defined

What are some alternatives I could do here?

Hi Joel,


getattr(item, string, False)

The False is a default if item doesn’t have an attribute named after string.

Wow, thank you. Now I feel like a dummy for not having thought of that. :slight_smile: