Is it allowed to use function results as default arguments ?

Discussion in 'Python' started by Stef Mientki, Jul 28, 2008.

  1. Stef Mientki

    Stef Mientki Guest

    hello,

    I've a perfect working procedure,
    at least as far I've tested it it works perfect.

    But I was just experimenting with inspect,
    and saw that the default argument was not parsed correctly.

    So I wonder if this is allowed:

    def Get_Relative_Path ( target, base=os.curdir ) :
    ...

    As inspect returns the following:

    (['target', 'base'], None, None, ('.',))

    thanks,
    Stef Mientki
     
    Stef Mientki, Jul 28, 2008
    #1
    1. Advertising

  2. Stef Mientki

    Simon Forman Guest

    On Jul 28, 1:28 pm, Stef Mientki <> wrote:
    > hello,
    >
    > I've a perfect working procedure,
    > at least as far I've tested it it works perfect.
    >
    > But I was just experimenting with inspect,
    > and saw that the default argument was not parsed correctly.
    >
    > So I wonder if this is allowed:
    >
    > def Get_Relative_Path ( target, base=os.curdir ) :
    >   ...
    >
    > As inspect returns the following:
    >
    > (['target', 'base'], None, None, ('.',))
    >
    > thanks,
    > Stef Mientki


    os.curdir is '.' on many platforms. What did you expect inspect to
    show?

    |>>> import os
    |>>> os.curdir
    '.'
     
    Simon Forman, Jul 28, 2008
    #2
    1. Advertising

  3. Stef Mientki

    Benjamin Guest

    On Jul 28, 3:28 pm, Stef Mientki <> wrote:
    > hello,
    >
    > I've a perfect working procedure,
    > at least as far I've tested it it works perfect.
    >
    > But I was just experimenting with inspect,
    > and saw that the default argument was not parsed correctly.
    >
    > So I wonder if this is allowed:
    >
    > def Get_Relative_Path ( target, base=os.curdir ) :


    Did you perhaps mean to say def Get_Relative_Path(target,
    base=os.getcwd()):
    >   ...
    >
    > As inspect returns the following:
    >
    > (['target', 'base'], None, None, ('.',))
    >
    > thanks,
    > Stef Mientki
     
    Benjamin, Jul 29, 2008
    #3
  4. Stef Mientki

    fred.haab Guest

    Well, others have answered the question, but I thought I'd throw in
    that it would be more pythonic to do something like:

    def Get_Relative_Path(target, base = None):
    if base is None:
    base = os.curdir
    ...
     
    fred.haab, Jul 30, 2008
    #4
  5. Stef Mientki

    Terry Reedy Guest

    fred.haab wrote:
    > Well, others have answered the question, but I thought I'd throw in
    > that it would be more pythonic to do something like:
    >
    > def Get_Relative_Path(target, base = None):
    > if base is None:
    > base = os.curdir
    > ...


    Since os.curdir is a constant, this is nonesensical. One only needs the
    dummy default when one wants an expression re-evaluated with each call.
     
    Terry Reedy, Jul 30, 2008
    #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. BRG
    Replies:
    11
    Views:
    6,426
  2. Lighter
    Replies:
    4
    Views:
    309
    Richard Herring
    Jul 11, 2006
  3. tutmann
    Replies:
    4
    Views:
    449
  4. jmborr
    Replies:
    1
    Views:
    435
    Stargaming
    Nov 3, 2007
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    517
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page