我正在实现字典的重复,所以我有2个哈希表,我试图设置旧的表来引用新的表。
我有以下几点:
def fart(hashTable):
hashTableTwo = mkHashTable(100)
hashTable = hashTableTwo
def main():
hashTableOne = mkHashTable(50)
fart(hashTableOne)
print(hashTableOne.size)
mkHashTable(50)创建一个对象HashTable,其大小为50。 这打印50,我希望它打印100。
我所拥有的似乎不起作用。有关如何使其工作的任何想法?我不允许使用全局变量或对象
答案 0 :(得分:0)
赋值(=)是名称绑定。
因此fart函数中的hashTable = hashTableTwo
不会更改原始的hashTable,它只是将fart函数中的变量名hashTable
绑定到hashTableTwo
对象。
查看此帖子Is Python call-by-value or call-by-reference? Neither.
解决方法取决于hashTable的数据结构/对象类型。 dict
提供了一些更新其值的方法。
def update_dict(bar):
bar = {'This will not':" work"}
def update_dict2(bar):
bar.clear()
bar.update(This_will="work")
foo={'This object is':'mutable'}
update_dict(foo)
print foo
>>> {'This object is': 'mutable'}
update_dict2(foo)
print foo
>>> {'This_will': 'work'}