Chen M
Chen M

Reputation: 1297

python argument inside function

I'm trying to pass argument inside function but no successes. the purpose of this function is to return xml tag this code doesn't work:

from bs4 import BeautifulSoup
def xmlTag(message):
 conf = open('timeLimit.conf').read().lower()
 for config in conf.splitlines():
    if config in conf.splitlines():
        data = BeautifulSoup(conf, "lxml")
        tag = data.message
        print(tag['msg'])

    break

xmlTag("fun2")

if i put fun2 instead of "message" variable, like this "tag = data.fun2" the code works please help what i"m doing wrong

Upvotes: 0

Views: 33

Answers (1)

jsbueno
jsbueno

Reputation: 110166

Try doing:

... tag = getattr(data, message) ...

getattr is the way of retrieving an attribute from an object when you have its name in a variable.

(Though your code have some other issues as well - that break statement where it is ensures your loop will terminate on the first iteration, for example)

Upvotes: 2

Related Questions