You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When trying to create multiple routes using a loop, I encountered the issue of the addresses from the previous iteration still being in the addresses variable. In address.py I believe I found the issue. Below is the current version of the code.
The issue here is that the addresses parameter is assigned a kwarg of an empty list. However, in python using a mutable object such as a list as a kwarg means that whenever the function is called the changes made to that parameter are stored in the function itself, thereby effecting the output every time the function is called. In this case, every time you create an Address object the addresses are stored in the addresses list and are not removed when you create another Address object. This can be avoided by either re-running the .py file if you're in an IDE or restarting the kernel if you're in Jupyter Notebook. However, making a simple adjustment in the source code will allow you to run the function multiple times and have the list of addresses update every time the function is called.
Here, instead of assigning a list to the address parameter, we assign it a None value and create the empty list variable inside the function itself. This way every time an Address object is created the addresses list will clear itself out before adding the new addresses.
Hope anyone who had the same trouble finds this method useful, and feel free to correct any mistakes in my explanation or provide an even simpler solution.
The text was updated successfully, but these errors were encountered:
When trying to create multiple routes using a loop, I encountered the issue of the addresses from the previous iteration still being in the addresses variable. In address.py I believe I found the issue. Below is the current version of the code.
The issue here is that the addresses parameter is assigned a kwarg of an empty list. However, in python using a mutable object such as a list as a kwarg means that whenever the function is called the changes made to that parameter are stored in the function itself, thereby effecting the output every time the function is called. In this case, every time you create an Address object the addresses are stored in the addresses list and are not removed when you create another Address object. This can be avoided by either re-running the .py file if you're in an IDE or restarting the kernel if you're in Jupyter Notebook. However, making a simple adjustment in the source code will allow you to run the function multiple times and have the list of addresses update every time the function is called.
Here, instead of assigning a list to the address parameter, we assign it a None value and create the empty list variable inside the function itself. This way every time an Address object is created the addresses list will clear itself out before adding the new addresses.
Hope anyone who had the same trouble finds this method useful, and feel free to correct any mistakes in my explanation or provide an even simpler solution.
The text was updated successfully, but these errors were encountered: