why are these not the same?

Discussion in 'Python' started by Lowell Kirsh, Jan 20, 2005.

  1. Lowell Kirsh

    Lowell Kirsh Guest

    On a webpage (see link below) I read that the following 2 forms are not
    the same and that the second should be avoided. They look the same to
    me. What's the difference?

    Lowell

    ----

    def functionF(argString="abc", argList = None):
    if argList is None: argList = []
    ...

    def functionF(argString="abc", argList=None):
    argList = argList or []
    ...

    http://www.ferg.org/projects/python_gotchas.html (number 5)
     
    Lowell Kirsh, Jan 20, 2005
    #1
    1. Advertising

  2. Lowell Kirsh wrote:
    > On a webpage (see link below) I read that the following 2 forms are not the same and that the
    > second should be avoided. They look the same to me. What's the difference?


    > def functionF(argString="abc", argList = None):
    > if argList is None: argList = []
    > ...
    >
    > def functionF(argString="abc", argList=None):
    > argList = argList or []
    > ...


    "is None" tests for None, "argList or" tests for a false value. None is
    false, but many non-None objects are also false.

    "should be avoided" sounds like overly zealous advice to me; use the
    latter form if you understand it.

    </F>
     
    Fredrik Lundh, Jan 20, 2005
    #2
    1. Advertising

  3. Lowell Kirsh

    Lowell Kirsh Guest

    D'oh I should've caught that myself.

    Thanks.

    Fredrik Lundh wrote:
    > Lowell Kirsh wrote:
    >
    >>On a webpage (see link below) I read that the following 2 forms are not the same and that the
    >>second should be avoided. They look the same to me. What's the difference?

    >
    >
    >>def functionF(argString="abc", argList = None):
    >> if argList is None: argList = []
    >> ...
    >>
    >>def functionF(argString="abc", argList=None):
    >> argList = argList or []
    >> ...

    >
    >
    > "is None" tests for None, "argList or" tests for a false value. None is
    > false, but many non-None objects are also false.
    >
    > "should be avoided" sounds like overly zealous advice to me; use the
    > latter form if you understand it.
    >
    > </F>
    >
    >
    >
     
    Lowell Kirsh, Jan 20, 2005
    #3
  4. Lowell Kirsh

    Duncan Booth Guest

    Lowell Kirsh wrote:

    > On a webpage (see link below) I read that the following 2 forms are not
    > the same and that the second should be avoided. They look the same to
    > me. What's the difference?
    >
    > Lowell
    >
    > ----
    >
    > def functionF(argString="abc", argList = None):
    > if argList is None: argList = []
    > ...
    >
    > def functionF(argString="abc", argList=None):
    > argList = argList or []
    > ...
    >
    > http://www.ferg.org/projects/python_gotchas.html (number 5)


    If functionF mutates its argument then these two will give different and
    possibly unexpected results. I suspect this is what they are hinting at:

    >>> def f1(s="abc", l=None):

    if l is None: l = []
    l.append(s)
    return '*'.join(l)

    >>> def f2(s="abc", l=None):

    l = l or []
    l.append(s)
    return '*'.join(l)

    >>> f1('foo', ['bar'])

    'bar*foo'
    >>> f2('foo', ['bar'])

    'bar*foo'
    >>> f1('foo', [])

    'foo'
    >>> f2('foo', [])

    'foo'

    So far the functions appear to operate identically. But:

    >>> myList = []
    >>> f1('foo', myList)

    'foo'
    >>> myList

    ['foo']
    >>> myList = []
    >>> f2('foo', myList)

    'foo'
    >>> myList

    []

    It looks as though f1 mutates its argument but f2 doesn't, until you try:

    >>> f2('foo', myList)

    'bar*foo'
    >>> myList

    ['bar', 'foo']
    >>>
    >>>


    So f2 mutates a non-empty list but leaves an empty list unchanged which is
    probably not what you intend and certainly confusing.
     
    Duncan Booth, Jan 20, 2005
    #4
    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. =?Utf-8?B?Q2hyaXM=?=

    Why are these tables not adding

    =?Utf-8?B?Q2hyaXM=?=, May 5, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,850
    Lucas Tam
    May 6, 2005
  2. =?Utf-8?B?Sm9u?=

    Why are these caracters not being display correct

    =?Utf-8?B?Sm9u?=, Aug 25, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    512
    John Timney \(MVP\)
    Aug 25, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    935
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,131
    Smokey Grindel
    Dec 2, 2006
  5. KevinSimonson
    Replies:
    9
    Views:
    380
    Jorgen Grahn
    Nov 10, 2010
Loading...

Share This Page