namespace issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Nogradi

    #1

    namespace issue

    I would like to give the same name to a keyword argument of a class
    method as the name of a function, with the function and the class
    living in the same namespace and the class method using the
    aforementioned function. So far I've been unsuccesfully trying to go
    along these lines:

    def great_name( x ):
    return x.upper( )

    class myclass:
    def mymethod( self, great_name=Fals e ):
    if great_name:
    return great_name( 'something' )
    else:
    return 'something'

    This would fail, because in the namespace of mymethod great_name is a
    local variable and is not a callable. So I tried to modify the class
    like this:

    class myclass:
    def mymethod( self, great_name=Fals e ):
    great_name_ = great_name
    del great_name
    if great_name_:
    return great_name( 'something' )
    else:
    return 'something'

    in the hope of the del statement only removing the local variable util
    but still remembering the great_name function from outside, but this
    didn't work either. So my question is if it was possible to do this at
    all?

    The reason for giving the same name is a usability issue of my module,
    I would like both the keyword argument and the function to be visible
    by the user and the name I would like to give them describes very well
    what they are doing. That is also the reason why I don't want to hide
    the great_name function in the class as a method.
  • Steven Bethard

    #2
    Re: namespace issue

    Daniel Nogradi wrote:[color=blue]
    > I would like to give the same name to a keyword argument of a class
    > method as the name of a function, with the function and the class
    > living in the same namespace and the class method using the
    > aforementioned function. So far I've been unsuccesfully trying to go
    > along these lines:
    >
    > def great_name( x ):
    > return x.upper( )
    >
    > class myclass:
    > def mymethod( self, great_name=Fals e ):
    > if great_name:
    > return great_name( 'something' )
    > else:
    > return 'something'
    >[/color]
    [color=blue][color=green][color=darkred]
    >>> def great_name(x):[/color][/color][/color]
    .... return x.upper()
    ....[color=blue][color=green][color=darkred]
    >>> class myclass(object) :[/color][/color][/color]
    .... def mymethod(self, great_name=Fals e):
    .... if great_name:
    .... return globals()['great_name']('something')
    .... else:
    .... return 'something'
    ....[color=blue][color=green][color=darkred]
    >>> myclass().mymet hod()[/color][/color][/color]
    'something'[color=blue][color=green][color=darkred]
    >>> myclass().mymet hod(True)[/color][/color][/color]
    'SOMETHING'


    STeVe

    Comment

    • Steven D'Aprano

      #3
      Re: namespace issue

      On Thu, 13 Apr 2006 22:59:52 +0200, Daniel Nogradi wrote:
      [color=blue]
      > I would like to give the same name to a keyword argument of a class
      > method as the name of a function, with the function and the class
      > living in the same namespace and the class method using the
      > aforementioned function.[/color]

      That's a problem right there. As soon as you find yourself needing to
      distinguish between "great_name the function" and "great_name the
      argument", you have a potential source of API confusion, no matter how
      great the name is.

      But if you absolutely must:

      def _gn(x):
      return x.upper()

      great_name = _gn

      class myclass:
      def mymethod(self, great_name=Fals e):
      if great_name:
      return _gn('something' )
      else:
      return 'something'




      --
      Steven.

      Comment

      • Daniel Nogradi

        #4
        Re: namespace issue

        > def _gn(x):[color=blue]
        > return x.upper()
        >
        > great_name = _gn
        >
        > class myclass:
        > def mymethod(self, great_name=Fals e):
        > if great_name:
        > return _gn('something' )
        > else:
        > return 'something'[/color]

        [color=blue][color=green][color=darkred]
        > >>> def great_name(x):[/color][/color]
        > ... return x.upper()
        > ...[color=green][color=darkred]
        > >>> class myclass(object) :[/color][/color]
        > ... def mymethod(self, great_name=Fals e):
        > ... if great_name:
        > ... return globals()['great_name']('something')
        > ... else:
        > ... return 'something'
        > ...[color=green][color=darkred]
        > >>> myclass().mymet hod()[/color][/color]
        > 'something'[color=green][color=darkred]
        > >>> myclass().mymet hod(True)[/color][/color]
        > 'SOMETHING'[/color]



        Thanks a lot for both suggestions, they were the things I was looking for.

        Comment

        Working...