namespace issue

Discussion in 'Python' started by Daniel Nogradi, Apr 13, 2006.

  1. 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=False ):
    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=False ):
    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.
     
    Daniel Nogradi, Apr 13, 2006
    #1
    1. Advertising

  2. Daniel Nogradi wrote:
    > 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=False ):
    > if great_name:
    > return great_name( 'something' )
    > else:
    > return 'something'
    >


    >>> def great_name(x):

    .... return x.upper()
    ....
    >>> class myclass(object):

    .... def mymethod(self, great_name=False):
    .... if great_name:
    .... return globals()['great_name']('something')
    .... else:
    .... return 'something'
    ....
    >>> myclass().mymethod()

    'something'
    >>> myclass().mymethod(True)

    'SOMETHING'


    STeVe
     
    Steven Bethard, Apr 14, 2006
    #2
    1. Advertising

  3. On Thu, 13 Apr 2006 22:59:52 +0200, Daniel Nogradi wrote:

    > 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.


    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=False):
    if great_name:
    return _gn('something')
    else:
    return 'something'




    --
    Steven.
     
    Steven D'Aprano, Apr 14, 2006
    #3
  4. > def _gn(x):
    > return x.upper()
    >
    > great_name = _gn
    >
    > class myclass:
    > def mymethod(self, great_name=False):
    > if great_name:
    > return _gn('something')
    > else:
    > return 'something'



    > >>> def great_name(x):

    > ... return x.upper()
    > ...
    > >>> class myclass(object):

    > ... def mymethod(self, great_name=False):
    > ... if great_name:
    > ... return globals()['great_name']('something')
    > ... else:
    > ... return 'something'
    > ...
    > >>> myclass().mymethod()

    > 'something'
    > >>> myclass().mymethod(True)

    > 'SOMETHING'




    Thanks a lot for both suggestions, they were the things I was looking for.
     
    Daniel Nogradi, Apr 14, 2006
    #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. Èý¹â
    Replies:
    1
    Views:
    573
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,136
  3. Anonymous
    Replies:
    3
    Views:
    534
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    452
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,368
    Howard
    Nov 29, 2004
Loading...

Share This Page