Arjay
08-18-2004, 10:23 AM
Hi all,
I have been working through 'Learning Python' by O'Reilly, unfortunately it is the first edition (1999), a second edition has been released since then. I can't seem to get one of the exercises in the 'functions' chapter to work properly. Basically it is supposed to copy its dictionary argument and should return a new dictionary with all the items in its argument. The code below is from the answer section at the back of the book.
#!/usr/bin/python
# copydict
def copyDict(old):
new = {}
for key in old.keys():
new[key] = old[key]
return new
# addDict
def addDict(d1, d2):
new = {}
for key in dl.keys():
new[key] = dl[key]
for key in d2.keys():
new[key] = d2[key]
return new
I have saved this module as 'dict.py' and made it executable. From there i am supposed to pass arguments from the interactive prompt...as stated in the answer section.
% python
>>> from dict import *
>>> d = {1:1, 2:2}
>>> e = copyDict(d)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "dict.py", line 6, in copyDict
global old
NameError: global name 'old' is not defined
This is the problem, I'm a bit confused with this message. The only other language i have knowledge in is Java, I'm trying to pick up a scripting language. I didn't think the 'old' parameter had to be defined in Python because it is a dynamically typed language, i thought it was assigned at runtime. Then the scope thing, to me 'old' is local to 'copyDict' and not global, but i suppose it is global to the for loop. Anyway i changed the 'old' parameter and gave it a value..
# copydict
def copyDict(old = 0):
new = {}
for key in old.keys():
new[key] = old[key]
return new
Then i could carry on with the rest of the exercise which turned out as stated in the book, although the addDict calls still don't work and keep giving an error..
% python
>>> from dict import *
>>> d = {1:1, 2:2}
>>> e = copyDict(d)
>>>d[2] = '?'
>>>d
{1: 1, 2: '?'}
>>>e
{1: 1, 2: 2}
>>> x = {1:1}
>>> y = {2:2}
>>> z = addDict(x, y)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "dict.py", line 13, in addDict
for key in dl.keys():
NameError: global name 'dl' is not defined
Could anyone possibly throw some light on these errors as i am a bit confused. Would it have anything to do with using an old book (1999) and using Python 2.3.4. Thanks for any help, it's fully appreciated.
Cheers :)
I have been working through 'Learning Python' by O'Reilly, unfortunately it is the first edition (1999), a second edition has been released since then. I can't seem to get one of the exercises in the 'functions' chapter to work properly. Basically it is supposed to copy its dictionary argument and should return a new dictionary with all the items in its argument. The code below is from the answer section at the back of the book.
#!/usr/bin/python
# copydict
def copyDict(old):
new = {}
for key in old.keys():
new[key] = old[key]
return new
# addDict
def addDict(d1, d2):
new = {}
for key in dl.keys():
new[key] = dl[key]
for key in d2.keys():
new[key] = d2[key]
return new
I have saved this module as 'dict.py' and made it executable. From there i am supposed to pass arguments from the interactive prompt...as stated in the answer section.
% python
>>> from dict import *
>>> d = {1:1, 2:2}
>>> e = copyDict(d)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "dict.py", line 6, in copyDict
global old
NameError: global name 'old' is not defined
This is the problem, I'm a bit confused with this message. The only other language i have knowledge in is Java, I'm trying to pick up a scripting language. I didn't think the 'old' parameter had to be defined in Python because it is a dynamically typed language, i thought it was assigned at runtime. Then the scope thing, to me 'old' is local to 'copyDict' and not global, but i suppose it is global to the for loop. Anyway i changed the 'old' parameter and gave it a value..
# copydict
def copyDict(old = 0):
new = {}
for key in old.keys():
new[key] = old[key]
return new
Then i could carry on with the rest of the exercise which turned out as stated in the book, although the addDict calls still don't work and keep giving an error..
% python
>>> from dict import *
>>> d = {1:1, 2:2}
>>> e = copyDict(d)
>>>d[2] = '?'
>>>d
{1: 1, 2: '?'}
>>>e
{1: 1, 2: 2}
>>> x = {1:1}
>>> y = {2:2}
>>> z = addDict(x, y)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "dict.py", line 13, in addDict
for key in dl.keys():
NameError: global name 'dl' is not defined
Could anyone possibly throw some light on these errors as i am a bit confused. Would it have anything to do with using an old book (1999) and using Python 2.3.4. Thanks for any help, it's fully appreciated.
Cheers :)