python nested function not working fully

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

python nested function not working fully

Postby pythonnoob123 » Wed Jul 10, 2013 7:45 am

Here is a test program I made to demonstrate the problem I'm having in my actual program right now(my original is too long and this is easier to read/spot the problem)
Code: Select all
>>> def somefunc(s):
            s[0]=22

   
>>> def test(s):
     def helper(s):
         somefunc(s)
         s=s+[2,3]
         return s+[3,4]
      s=helper(s)

   
>>> a=[1,33,4,23]
>>> somefunc(a)
>>> a
[22, 33, 4, 23]
>>>#somefunc() works properly
>>> a=[1,33,4,23]
>>> test(a)
>>> a
[22, 33, 4, 23]
>>> a=[1,33,4,23]
>>> def helper(s):
      somefunc(s)
      s=s+[2,3]
      return s+[3,4]
>>>#helper function taken out of test
   
>>> helper(a)
[22, 33, 4, 23, 2, 3, 3, 4]

So I created a function test that has a nested function helper. Test is supposed to modify list (s) but not return any value, although the inner nested helper function returns a value. However, when I called test(a), it seems like the nested helper function works partially by implementing somefunc(s), but ignores the rest of the body. There's nothing wrong with helper itself since when I took it out, the function works as intended. Can someone tell me what's wrong? It's especially odd to me that the helper function in test was partially implemented, not ignored or implemented all the way. Oh and I'm new to this forum so if any indenting doesn't look right, it's because my code's indentation won't copy over from IDLE so I have to manually press the space bar on all lines of code that needs to be indented.
pythonnoob123
 
Posts: 4
Joined: Mon Jul 08, 2013 11:29 am

Re: python nested function not working fully

Postby Yoriz » Wed Jul 10, 2013 12:20 pm

The last part of the code you posted
Code: Select all
>>> helper(a)
[22, 33, 4, 23, 2, 3, 3, 4]
is the returned value of helper its not the value of a if you checked for the value of a it would also be
Code: Select all
[22, 33, 4, 23]

As soon as you assign a value to s inside the function helper it becomes a local variable to helper
so in the line
Code: Select all
s=s+[2,3]

s is assigned the value of a + [2,3] , a is not altered in place.
to alter a inside of helper use a's methods or index
ie. s.append, s.extend, s[i], s.insert ect.
or use the return value from helper and assign it to a
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1170
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: python nested function not working fully

Postby pythonnoob123 » Wed Jul 10, 2013 2:37 pm

Wait, I thought lists were a changeable data type. If I passed list "a" into a function as an argument, then "a" and s, name of the argument in the function, should refer to the same object, so whatever change I do to s inside the function should affect "a" as well. And that seems to be the case when somefunc was being implemented and changed the first value of list s.
pythonnoob123
 
Posts: 4
Joined: Mon Jul 08, 2013 11:29 am

Re: python nested function not working fully

Postby stranac » Wed Jul 10, 2013 3:04 pm

All of that is true, but you're not modifying the list, you are instead creating a new list.
If you want to modify it, use list methods such as .append() or .extend()
Using += might also work, but I'm not sure and can't really test.
Friendship is magic!

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

Re: python nested function not working fully

Postby Yoriz » Wed Jul 10, 2013 3:09 pm

Yes += works too
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1170
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: python nested function not working fully

Postby micseydel » Wed Jul 10, 2013 9:55 pm

stranac wrote:Using += might also work, but I'm not sure and can't really test.

Yoriz wrote:Yes += works too

Yeah, but don't use += here. In fact, unless performance is an issue, I wouldn't use .append() here, I'd create a new list and return it.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests