user18160
user18160

Reputation: 23

What this code is doing

Can anyone explain what this code is doing

dns = find_all(S("some value"))

index = [dns.index(x) for x in dns if x.web_element.get_attribute("name") == "some value"]

Any help is appreciated. Thanks

Upvotes: 1

Views: 58

Answers (1)

mba12
mba12

Reputation: 2792

The variable dns should be an iterable...meaning it is a list, tuple, etc.

The code walks through the dns list, each item in the list is placed in the variable x. x is an object with method web_element.get_attribute. The string "name" is pased to that object. If the returned value is "some value" then the result of dns.index(x) is placed in the variable someValue. someValue is then added to the newList.

It seems to me the code is creating a list of the indices of dns list objects that meet the criteria established by the if statement. The code below is perhaps more clear to someone new to python list comprehensions but does the same thing. Hope this helps.

newList = [] # empty list
for x in dns:
    if x.web_element.get_attribute("name") == "some value"
        someValue = dns.index(x)
        newList.append(someValue)

Upvotes: 1

Related Questions