Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > function scope

Reply
Thread Tools

function scope

 
 
Baris Demir
Guest
Posts: n/a
 
      02-02-2009
Hi everybody,

I am quite new to python and using it for my thesis. Luckily I found
out some kind of behavior surprising to me and so unwanted in my code. I
could not find any explanation, so solution for the code.
It is simply like this:

/*li = another_module.global_variable
f=simpleCut(li)

def simpleCut(d=dict()):
temp=d
for i in temp.keys():
if (temp[i] == .......) :
temp[i]=new_value
return temp
*/
here /*simpleCut(d=dict())*/ is a function that changes the value of the
key in the d if it satisfies conditions and returns the new dictionary.
what i do not understand is, when i checked the original dictionary
/*li,*/ i also found out that, the value of that key(operated in the if
statement) had also been changed. so, simply, the simpleCut function
also changes the variables of its own argument, even there is a 'temp'
variable. That is a scandal for my code

please tell me, if this is not possible and i am missing another thing
in my code, or if this kind of behavior is possible, what is the
philosophy of it, and how can i change it???

cheers

B.D.



 
Reply With Quote
 
 
 
 
Mike Kent
Guest
Posts: n/a
 
      02-03-2009
On Feb 2, 6:40*pm, Baris Demir <(E-Mail Removed)> wrote:

> def simpleCut(d=dict()):
> * * * *temp=d
> * * * *for i in temp.keys():
> * * * * * *if * *(temp[i] == .......) :
> * * * * * * * temp[i]=new_value
> return temp


You have been bitten by the shared default parameter noobie trap:
http://www.python.org/doc/faq/genera...etween-objects
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-03-2009
On Mon, 02 Feb 2009 16:37:07 -0800, Mike Kent wrote:

> On Feb 2, 6:40*pm, Baris Demir <(E-Mail Removed)> wrote:
>
>> def simpleCut(d=dict()):
>> * * * *temp=d
>> * * * *for i in temp.keys():
>> * * * * * *if * *(temp[i] == .......) :
>> * * * * * * * temp[i]=new_value
>> return temp

>
> You have been bitten by the shared default parameter noobie trap:


No, he has tripped over the assignment-doesn't-make-copies feature.
Ignore the default value, and consider passing in a dict:


def simpleCut(d=dict()):
* * temp=d
# ...

That doesn't make a temporary copy of d, it makes a new name that refers
to the same dictionary as d. So if you do this:

li = {'x': 1}
f = simpleCut(li)

then

assert f is li

will pass, and he will be surprised and dismayed to discover that
simpleCut() has side-effects.


Solution: make a copy of the input.


def simpleCut(d=dict()):
temp = d.copy()
for i in temp: # don't need to call keys()
if (temp[i] == ...):
temp[i] = new_value
return temp



--
Steven
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      02-03-2009
Mike Kent <(E-Mail Removed)> wrote:
>
>On Feb 2, 6:40*pm, Baris Demir <(E-Mail Removed)> wrote:
>
>> def simpleCut(d=dict()):
>> * * * *temp=d
>> * * * *for i in temp.keys():
>> * * * * * *if * *(temp[i] == .......) :
>> * * * * * * * temp[i]=new_value
>> return temp

>
>You have been bitten by the shared default parameter noobie trap:
>http://www.python.org/doc/faq/genera...etween-objects


Actually, he hasn't. His problem is more fundamental.

However, this is still a good thing for you to point out now and then.
--
Tim Roberts, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Baris Demir
Guest
Posts: n/a
 
      02-04-2009
Tim Roberts wrote:
> Mike Kent <(E-Mail Removed)> wrote:
>
>> On Feb 2, 6:40 pm, Baris Demir <(E-Mail Removed)> wrote:
>>
>>
>>> def simpleCut(d=dict()):
>>> temp=d
>>> for i in temp.keys():
>>> if (temp[i] == .......) :
>>> temp[i]=new_value
>>> return temp
>>>

>> You have been bitten by the shared default parameter noobie trap:
>> http://www.python.org/doc/faq/genera...etween-objects
>>

>
> Actually, he hasn't. His problem is more fundamental.
>
> However, this is still a good thing for you to point out now and then.
>


Thanks a lot for the replies guys. They all helped a lot. And, I guess,
I should read a lot more about python, it is not as simple as it seems
at the beginning, or for the beginners
cheers
BD

 
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
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments