trouble getting the copy module to work within a function

This is the place for queries that don't fit in any of the other categories.

trouble getting the copy module to work within a function

Postby The Nerd » Sat Jan 25, 2014 3:51 pm

Hello,

I am studying from the book "How to Think Like a Computer Scientist" (Allen Downey, Jeffrey Elkner, Chris Meyers). On page 134 the authors present the following function:

Code: Select all
def growRect(box, dwidth, dheight) :
          import copy
          newBox = copy.deepcopy(box)
          newBox.width = newBox.width + dwidth
          newBox.height = newBox.height + dheight
          return newBox


I am having problems getting this function to work :

Code: Select all
Python 2.7.3 (default, Sep 26 2013, 20:08:41)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====

>>> def growRect(box, dwidth, dheight):
      import copy
      newBox = copy.deepcopy(box)
      newBox.width = newBox.width + dwidth
      newBox.height = newBox.height + dheight
      return newBox

>>> class Point:
   pass

>>> class Rectangle:
   pass

>>> b1 = Rectangle()
>>> b1.width = 100.0
>>> b1.height = 200.0
>>>
>>> b1.corner = Point()
>>> b1.corner.x = 0.0
>>> b1.corner.y = 0.0
>>>
>>> b1.width
100.0
>>>
>>> growRect(b1, 75, 30)
<__main__.Rectangle instance at 0x9d2906c>
>>> newBox
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    newBox
NameError: name 'newBox' is not defined


So in spite of having called the function "growRect" on b1, an error message states that "newBox" is not defined, when it should be created by the "growRect" function.

Can anyone shed some light on why this is not working the way I would expect it to?
Last edited by stranac on Sat Jan 25, 2014 4:11 pm, edited 1 time in total.
Reason: First post lock.
The Nerd
 
Posts: 2
Joined: Fri Jan 24, 2014 10:30 pm

Re: trouble getting the copy module to work within a functio

Postby stranac » Sat Jan 25, 2014 4:15 pm

newBox is local to the function it was created in.
That name does not exist outside of the function.

You need to assign a name to the value returned from the function.
Something like this should work:
Code: Select all
newBox = growRect(b1, 75, 30)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: trouble getting the copy module to work within a functio

Postby metulburr » Sat Jan 25, 2014 4:16 pm

newBox is a local variable inside the function growRect(). When you return it, you have to return it to a variable in the scope you are in. Currently, you are not.
Code: Select all
>>> growRect(b1, 75, 30)

change that to:
Code: Select all
>>> newBox = growRect(b1, 75, 30)


EDIT:
ahh stranac beat me to it
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1299
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: trouble getting the copy module to work within a functio

Postby The Nerd » Sat Jan 25, 2014 11:47 pm

Hi stranac and metulburr,

Thanks for your advice. I tried out assigning a name to the value returned from the function. And of course it worked.

:mrgreen:
The Nerd
 
Posts: 2
Joined: Fri Jan 24, 2014 10:30 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests