Alternative to "assign to a function call" in a python

Posted by Pythonista's Apprentice on Stack Overflow See other posts from Stack Overflow or by Pythonista's Apprentice
Published on 2012-03-26T05:14:56Z Indexed on 2012/03/26 5:29 UTC
Read the original article Hit count: 181

Filed under:
|
|

I'm trying to solve this newbie puzzle:

I've created this function:

def bucket_loop(htable, key):
    bucket = hashtable_get_bucket(htable, key)
    for entry in bucket:
        if entry[0] == key:          
            return entry[1]            
        else:        
            return None

And I have to call it in two other functions (bellow) in the following way: to change the value of the element entry[1] or to append to this list (entry) a new element. But I can't do that calling the function bucket_loop the way I did because "you can't assign to function call" (assigning to a function call is illegal in Python). What is the alternative (most similar to the code I wrote) to do this (bucket_loop(htable, key) = value and hashtable_get_bucket(htable, key).append([key, value]))?

def hashtable_update(htable, key, value):
    if bucket_loop(htable, key) != None:
        bucket_loop(htable, key) = value
    else:
        hashtable_get_bucket(htable, key).append([key, value])

def hashtable_lookup(htable, key):
    return bucket_loop(htable, key)

Thanks, in advance, for any help!

This is the rest of the code to make this script works:

def make_hashtable(size):
    table = []
    for unused in range(0, size):
        table.append([])
    return table

def hash_string(s, size):
    h = 0
    for c in s:
         h = h + ord(c)
    return h % size

def hashtable_get_bucket(htable, key):
    return htable[hash_string(key, len(htable))]

Similar question (but didn't help me): Python: Cannot Assign Function Call

© Stack Overflow or respective owner

Related posts about python

Related posts about list