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

Search Discussions

  • Daniel Nogradi at Apr 13, 2006 at 9:06 pm

    Ooops, there was a typo in my previous mail:

    in the hope of the del statement only removing the local variable util
    ^^^^^
    the above line should be:

    in the hope of the del statement only removing the local variable great_name

    ^^^^^^^^^^^^
  • Steven Bethard at Apr 14, 2006 at 12:14 am

    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


    From http Fri Apr 14 02:36:49 2006
    From: http (Paul Rubin)
    Date: 13 Apr 2006 17:36:49 -0700
    Subject: Forms with multiple submit buttons vs 'form' objects with single
    'submit' methods
    References: <1144956412.286214.316800@u72g2000cwu.googlegroups.com>
    Message-ID: <7xbqv5c7ry.fsf@ruckus.brouhaha.com>

    neil.fitzgerald at ic.ac.uk writes:
    Here's my question: Suppose a form has more than one submit button.
    Now the COM 'form' object has a 'submit' method that doesn't take any
    arguments, so how do I tell it which button I want to press?
    What difference does it make? Don't they all do the same thing?
  • Steven D'Aprano at Apr 14, 2006 at 2:59 am

    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.
  • Daniel Nogradi at Apr 14, 2006 at 9:39 am

    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 13, '06 at 8:59p
activeApr 14, '06 at 9:39a
posts5
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase