Is it allowed to use function results as default arguments ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stef Mientki

    Is it allowed to use function results as default arguments ?

    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_Pa th ( target, base=os.curdir ) :
    ...

    As inspect returns the following:

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

    thanks,
    Stef Mientki
  • Simon Forman

    #2
    Re: Is it allowed to use function results as default arguments ?

    On Jul 28, 1:28 pm, Stef Mientki <stef.mien...@g mail.comwrote:
    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_Pa th ( 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
    '.'

    Comment

    • Benjamin

      #3
      Re: Is it allowed to use function results as default arguments ?

      On Jul 28, 3:28 pm, Stef Mientki <stef.mien...@g mail.comwrote:
      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_Pa th ( target, base=os.curdir ) :
      Did you perhaps mean to say def Get_Relative_Pa th(target,
      base=os.getcwd( )):
        ...
      >
      As inspect returns the following:
      >
      (['target', 'base'], None, None, ('.',))
      >
      thanks,
      Stef Mientki

      Comment

      • fred.haab

        #4
        Re: Is it allowed to use function results as default arguments ?

        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_Pa th(target, base = None):
        if base is None:
        base = os.curdir
        ...

        Comment

        • Ben Finney

          #5
          Re: Is it allowed to use function results as default arguments ?

          "fred.haab" <fred.haab@gmai l.comwrites:
          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_Pa th(target, base = None):
          if base is None:
          base = os.curdir
          ...
          Even more Pythonic would be to name the function by the style guide
          (PEP 8):

          def get_relative_pa th(target, base=None):
          if base is None:
          base = os.curdir
          # …

          --
          \ “If trees could scream, would we be so cavalier about cutting |
          `\ them down? We might, if they screamed all the time, for no good |
          _o__) reason.” —Jack Handey |
          Ben Finney

          Comment

          • Terry Reedy

            #6
            Re: Is it allowed to use function results as default arguments ?



            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_Pa th(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.

            Comment

            Working...