Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why I have to do a deepcopy to the argument?

Reply
Thread Tools

Why I have to do a deepcopy to the argument?

 
 
Dimitris Leventeas
Guest
Posts: n/a
 
      06-16-2010
Hi!

I am trying to understand Python's method of passing arguments, the references
to objects etc. I fail to grasp why in populate_trie I have to make a deepcopy
of trie locally instead of just referencing to it.

If it makes any difference, I use Python 3.


from copy import deepcopy

def access_trie(d, sequence, position=None):
"""
Access the dictionary which is referred by applying consequently each
term of the sequence. In a more python terms, if sequence is 'key',
access: d['k']['e']['y'] Assume that the dictionary is at the
`position` of a list, if `position` is an argument.

>>> a = {'k': [0, {'a': [0, {'l': [0, {'o': [1, {}]}]}]}]}
>>> access_trie(a, 'kal', 1)

{'o': [1, {}]}
>>> access_trie(a, 'kalo', 1)

{}
>>> a = {'d': {'i': {'m': {'i': {'t': {'r': {'i': {'s': 1}}}}}}}}
>>> access_trie(a, 'dimitr')

{'i': {'s': 1}}
>>> access_trie(a, '')

{'d': {'i': {'m': {'i': {'t': {'r': {'i': {'s': 1}}}}}}}}
>>> access_trie(a, 'dimitris')

1
>>> b = access_trie(a, 'dimitr')
>>> b['o'] = {'s': 1}
>>> a

{'d': {'i': {'m': {'i': {'t': {'r': {'i': {'s': 1}, 'o': {'s': 1}}}}}}}}

"""

for c in sequence:
d = d[c]
if position is not None:
d = d[position]

return d


def populate_trie(trie, sequence, position=None):
"""
Populate a trie.

Assume that the counter is always at `position` 0 while the `position`
of the dictionary is the last one.

>>> trie = {}
>>> populate_trie(trie, 'taspython')

{'t': {'a': {'s': {'p': {'y': {'t': {'h': {'o': {'n': {}}}}}}}}}}
>>> trie = {}
>>> populate_trie(trie, 'kalo', 1)

{'k': [1, {'a': [1, {'l': [1, {'o': [1, {}]}]}]}]}
>>> trie = {}
>>> populate_trie(trie, 'heh', 2)

{'h': [1, 0, {'e': [1, 0, {'h': [1, 0, {}]}]}]}
>>> trie = {}
>>> trie = populate_trie(trie, 'heh', 1)
>>> populate_trie(trie, 'hah', 1)

{'h': [2, {'a': [1, {'h': [1, {}]}], 'e': [1, {'h': [1, {}]}]}]}


"""

if (position is not None) and (position >= 1):
embedded_obj = [0] * position
embedded_obj.append({})
else:
embedded_obj = {}

d = deepcopy(trie)
d2 = d
for i, character in enumerate(sequence):
d2 = access_trie(d, sequence[:i], position)
if character not in d2:
if position is None:
d2[character] = deepcopy(embedded_obj)
else:
d2[character] = d2.get(character, deepcopy(embedded_obj))
d2[character][0] += 1
elif position is not None:
d2[character][0] += 1

return d



Best regargs,
Dimitris Leventeas
--
Dimitris Leventeas
http://students.ceid.upatras.gr/~lebenteas/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why aren't copy and deepcopy in __builtins__? John Ladasky Python 9 03-31-2011 12:35 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Legitimacy of deepcopy Eugeni Doljenko Python 2 06-07-2004 09:38 PM
Deepcopy on XML node in python2.2 problem sag@hydrosphere.com Python 0 09-08-2003 07:38 PM



Advertisments