How do I find out if a integer variable in python has an out of scope hidden value? -
in code here how force static methods use local not global variables in python?
i have variable being passed between methods besides 1 in question. variable current_line
def line_tocsv(csv_dict={}, line={}, current_line=0): csv_line, current_line = linehandler.create_csv(current_line, kline) if current_line in csv_dict: csv_dict[current_line].update(csv_line) else: csv_dict[current_line] = csv_line return csv_dict
when code run, produced output simmilar this
>>> a={0:{"aa":1,"bb":"wasd"},1:{"aa":1,"bb":"wasd"}} >>> {0: {'aa': 1, 'bb': 'wasd'}, 1: {'aa': 1, 'bb': 'wasd'}} >>> a[1].update({"cc":"foo"}) >>> {0: {'aa': 1, 'cc': 'foo' 'bb': 'wasd'}, 1: {'aa': 1, 'cc': 'foo', 'bb': 'wasd'}}
how make csv_line dict entered 1 sub dict?! changing variable names not work , suspect because references passed between functions, dont know enough python know references go , order of ops etc is
first, don't use mutable default arguments
def line_tocsv(csv_dict=none, ....): if not csv_dict: csv_dict = {} ....
next, use copy of dict, not pointer:
csv_dict[current_line] = csv_line.copy()
edit:
try this:
>>> {1: 2, 3: 4} >>> b = {1:a,2:a} >>> b {1: {1: 2, 3: 4}, 2: {1: 2, 3: 4}} >>> b[1][7] = 8 >>> b {1: {1: 2, 3: 4, 7: 8}, 2: {1: 2, 3: 4, 7: 8}}
if want use value of dict instead of pointer:
>>> {1: 2, 3: 4} >>> b = {1:a.copy(),2:a.copy()} >>> b {1: {1: 2, 3: 4}, 2: {1: 2, 3: 4}} >>> b[1][7] = 8 >>> b {1: {1: 2, 3: 4, 7: 8}, 2: {1: 2, 3: 4}}
Comments
Post a Comment