Eugenio Herrera Eugenio Herrera - 4 months ago 7
Python Question

Value entered in a function changed by the default value

So I created this function:

def bs_obj(url, lan="html.parser"):
try:
html = urlopen(url)
bsObj = BeautifulSoup(html, lan)
print(lan)
return bsObj
except HTTPError as e:
print(e)


Now, if I call the function with the next code:
object = bs_obj(html, "lxml")
, the console prints
html.parser
. Same goes if the code is
object = bs_obj(html, lan="lxml")
. What's going on?

EDIT: (SOLVED) I'm ashamed. I was calling
bs_obj(html)
some lines before the codeline I used as example.

Answer

I believe you are running the wrong file. For reference.

def bs_obj(lan="html.parser"):
    print(lan)

if __name__ == "__main__":
    bs_obj()
    bs_obj("lxml")
    bs_obj(lan='html5.parser')

Correctly outputs

html.parser
lxml
html5.parser