hky404 hky404 - 3 years ago 197
Python Question

AttributeError: 'set' object has no attribute 'b'

I have the following code:

N1 = int(input())
a = set(list(map(int, input().split())))
N2 = int(input())
for i in range(N2):
b = input().split()
c = set(list(map(int, input().split())))

With typical input, the list
looks like this:

b = ['intersection_update', '10']

What is the issue with
? Apparently I am not evaluating it correctly.

The concept seems fine, but it seems like set
is not able to take an attribute which is actually an element of a list.

what I want to evaluate is:


Here's the error I get:

Traceback (most recent call last):
File "", line 7, in
AttributeError: 'set' object has no attribute 'b'

Answer Source

You can't do that kind of indirect attribute access using the dot operator in Python. Use getattr() instead:

>>> a = {1, 2, 3, 4, 5}
>>> c = {3, 4, 5, 6, 7}
>>> b = ['intersection_update', '10']
>>> getattr(a, b[0])(c)
>>> a
{3, 4, 5}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download