parameter name conflict. How to solve?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bo Peng

    parameter name conflict. How to solve?

    Dear list,

    If you ask: why do you choose these names? The answer is: they need to
    be conformable with other functions, parameter names.

    I have a function that pretty much like:

    def output(output=' '):
    print output

    and now in another function, I need to call output function, with again
    keyword parameter output

    def func(output='') :
    output(output=o utput)

    Naturally, I get 'str' object is not callable. Is there a way to tell
    func that the first output is actually a function? (like in C++,
    ::output(output ) )

    Thanks.
    Bo
  • Steven Bethard

    #2
    Re: parameter name conflict. How to solve?

    Bo Peng wrote:[color=blue]
    > Dear list,
    >
    > If you ask: why do you choose these names? The answer is: they need to
    > be conformable with other functions, parameter names.
    >
    > I have a function that pretty much like:
    >
    > def output(output=' '):
    > print output
    >
    > and now in another function, I need to call output function, with again
    > keyword parameter output
    >
    > def func(output='') :
    > output(output=o utput)
    >
    > Naturally, I get 'str' object is not callable. Is there a way to tell
    > func that the first output is actually a function?[/color]

    Yes, but you probably don't want to go that way. Can you explicitly
    indicate the location of the 'output' function? e.g.:

    py> def output(output=' '):
    .... print output
    ....
    py> def func(output='') :
    .... __import__(__na me__).output(ou tput)
    ....
    py> func('abc')
    abc

    or possibly:

    py> def func(output='') :
    .... globals()['output'](output)
    ....
    py> func('abc')
    abc

    This is easier if output is in another module -- you can just write
    something like:
    outputmodule.ou tput(output)

    STeVe

    Comment

    • Daniel Dittmar

      #3
      Re: parameter name conflict. How to solve?

      Bo Peng wrote:[color=blue]
      > def func(output='') :
      > output(output=o utput)
      >
      > Naturally, I get 'str' object is not callable. Is there a way to tell
      > func that the first output is actually a function? (like in C++,
      > ::output(output ) )[/color]

      output_alias = output

      def func (output=''):
      output_alias(ou tput=output)

      Daniel

      Comment

      • Kent Johnson

        #4
        Re: parameter name conflict. How to solve?

        Bo Peng wrote:[color=blue]
        > def func(output='') :
        > output(output=o utput)
        >
        > Naturally, I get 'str' object is not callable. Is there a way to tell
        > func that the first output is actually a function? (like in C++,
        > ::output(output ) )[/color]

        You could use a default argument:
        def func(output='', output_fn=outpu t):
        output_fn(outpu t=output)

        Kent

        Comment

        • Pierre Barbier de Reuille

          #5
          Re: parameter name conflict. How to solve?

          Bo Peng a écrit :[color=blue]
          > Dear list,
          >
          > If you ask: why do you choose these names? The answer is: they need to
          > be conformable with other functions, parameter names.
          >
          > I have a function that pretty much like:
          >
          > def output(output=' '):
          > print output
          >
          > and now in another function, I need to call output function, with again
          > keyword parameter output
          >
          > def func(output='') :
          > output(output=o utput)
          >
          > Naturally, I get 'str' object is not callable. Is there a way to tell
          > func that the first output is actually a function? (like in C++,
          > ::output(output ) )
          >
          > Thanks.
          > Bo[/color]

          What I'd suggest is :

          def func(output='') :
          gobals()["output"](output=output)

          that way, the function resolution is still dynamic, but you explicitly
          ask for a name global and not local ...

          Pierre

          Comment

          • Bo Peng

            #6
            Re: parameter name conflict. How to solve?

            Kent Johnson wrote:[color=blue]
            > Bo Peng wrote:
            >[color=green]
            >> def func(output='') :
            >> output(output=o utput)
            >>
            >> Naturally, I get 'str' object is not callable. Is there a way to tell
            >> func that the first output is actually a function? (like in C++,
            >> ::output(output ) )[/color]
            >
            >
            > You could use a default argument:
            > def func(output='', output_fn=outpu t):
            > output_fn(outpu t=output)
            >
            > Kent[/color]

            Thank everyone for the quick responses. All methods work in general.
            Since func() has to take the same parameter set as some other functins,
            I can not use func(output='', output_fn=outpu t). "output_alias=o utput"
            etc are fine.

            Bo

            Comment

            • Duncan Booth

              #7
              Re: parameter name conflict. How to solve?

              Bo Peng wrote:
              [color=blue]
              > Thank everyone for the quick responses. All methods work in general.
              > Since func() has to take the same parameter set as some other functins,
              > I can not use func(output='', output_fn=outpu t). "output_alias=o utput"
              > etc are fine.[/color]

              One suggestion that I haven't seen so far:

              Where does this mysteriously named function 'output' come from? If it is
              defined in another module, then simply call it qualified by the module
              name.

              i.e. if the code looks like:

              from othermodule import output

              def func(output='') :
              output(output=o utput)

              change it to:

              import othermodule

              def func(output='') :
              othermodule.out put(output=outp ut)

              Comment

              • Greg Ewing

                #8
                Re: parameter name conflict. How to solve?

                Bo Peng wrote:
                [color=blue]
                > def func(output='') :
                > output(output=o utput)[/color]

                Another solution that hasn't been mentioned yet:

                def func(**kwds):
                output(output = kwds['output'])

                You might want to do some more checking on the
                contents of kwds to make sure it doesn't
                contain any other nonsense parameters.

                --
                Greg Ewing, Computer Science Dept,
                University of Canterbury,
                Christchurch, New Zealand

                Comment

                Working...