[python-win32] Retaining a value during a recursive function

Tim Roberts timr at probo.com
Fri Nov 17 21:02:09 CET 2006


James Matthews wrote:
> The function just overwrites the list in my function is there anyway i
> can fix this!
> def find_all_items(site):
>     site = urllib.urlopen(site).read()
>     all_items = re.findall(r'watch\.asp\?\w+\=\w*\&\w*\=\w+',site)
>     next_page = re.findall(r'Watches\.asp\?\w+\=[0-9]+\&pg\=\w+',site)
>     try:
>         find_all_items(urllib.basejoin("<some url>",next_page[0]))
>     except IndexError:
>         pass
>     return remove_dups(all_items) # ,<---- this removes all the
> duplicate items

Actually, it doesn't overwrite the list.  You just don't do anything
with the list that is returned from the call inside the function. 
Remember that site, all_items, and next_page are LOCAL variable; they
exist only inside that one function call.  Every time you call
find_all_items, you get a brand new instance of all_items.

  try:
    all_items.extend( find_all_items( urllib.basejoin("<some
url>",next_page[0] ) )

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list