function scope

Discussion in 'Python' started by Baris Demir, Feb 2, 2009.

  1. Baris Demir

    Baris Demir Guest

    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 == .......) :
    temp=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.
     
    Baris Demir, Feb 2, 2009
    #1
    1. Advertising

  2. Baris Demir

    Mike Kent Guest

    Mike Kent, Feb 3, 2009
    #2
    1. Advertising

  3. On Mon, 02 Feb 2009 16:37:07 -0800, Mike Kent wrote:

    > On Feb 2, 6:40 pm, Baris Demir <> wrote:
    >
    >> def simpleCut(d=dict()):
    >>        temp=d
    >>        for i in temp.keys():
    >>            if    (temp == .......) :
    >>               temp=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 == ...):
    temp = new_value
    return temp



    --
    Steven
     
    Steven D'Aprano, Feb 3, 2009
    #3
  4. Baris Demir

    Tim Roberts Guest

    Mike Kent <> wrote:
    >
    >On Feb 2, 6:40 pm, Baris Demir <> wrote:
    >
    >> def simpleCut(d=dict()):
    >>        temp=d
    >>        for i in temp.keys():
    >>            if    (temp == .......) :
    >>               temp=new_value
    >> return temp

    >
    >You have been bitten by the shared default parameter noobie trap:
    >http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-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,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Feb 3, 2009
    #4
  5. Baris Demir

    Baris Demir Guest

    Tim Roberts wrote:
    > Mike Kent <> wrote:
    >
    >> On Feb 2, 6:40 pm, Baris Demir <> wrote:
    >>
    >>
    >>> def simpleCut(d=dict()):
    >>> temp=d
    >>> for i in temp.keys():
    >>> if (temp == .......) :
    >>> temp=new_value
    >>> return temp
    >>>

    >> You have been bitten by the shared default parameter noobie trap:
    >> http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-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
     
    Baris Demir, Feb 4, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    961
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    672
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    488
  4. Xah Lee
    Replies:
    0
    Views:
    2,248
    Xah Lee
    Feb 26, 2009
  5. Andrew Falanga
    Replies:
    2
    Views:
    202
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page