Home >Backend Development >Python Tutorial >Why creating a variable and using that variable as reference can lead to confusion?
In a Python script, I wanted to test different HTML strings using the same logic. My approach was to loop through a range to create multiple instances of the HTML string variables, but it wasn't working as expected.
# DO NOT DO THIS for i in range(1, 5): html = f"html{i}" soup = BeautifulSoup(html, "html.parser") print('----', soup)
The behavior I was observing is due to the way the formatted string f"html{i}" is interpreted. In my code, f"html{i}" evaluates to the literals "html1", "html2", "html3", and "html4" rather than the contents of variables named html1, html2, etc.
Python does not automatically replace f"html{i}" with the value of the variable whose name is dynamically created such as html1 or html2. Instead, it evaluates the string as a fixed pattern comprised of the prefix "html" followed by the value of i.
If I want to use the contents of pre-defined variables html1, html2, etc., I need to explicitly retrieve their values, for example using a dictionary to map string names to their actual content.
Here's an example illustrating this:
from bs4 import BeautifulSoup # Define the variables html1 = "Test 1" html2 = "Test 2" html3 = "Test 3" html4 = "Test 4" # Store them in a dictionary for easy access html_dict = { "html1": html1, "html2": html2, "html3": html3, "html4": html4 } # Iterate and process each html content for i in range(1, 5): key = f"html{i}" html = html_dict[key] soup = BeautifulSoup(html, "html.parser") print('----', soup)
Define Variables:
Dictionary for Variable Lookup:
Iterate Over Keys:
Parse and Print:
---- Test 1 ---- Test 2 ---- Test 3 ---- Test 4
This approach dynamically accesses the content of the variables based on the iteration index and correctly prints the intended content.
The above is the detailed content of Why creating a variable and using that variable as reference can lead to confusion?. For more information, please follow other related articles on the PHP Chinese website!