FAQ
Dear All,

I have a simple class
class hello:
def world(self):
return 'hello'
def test(self,arg):
return self.arg

When I want to do is:
hello.test('world')
'hello'

i.e. pass the method name as an argument. How should I do this?

Thanks

Colin

Search Discussions

  • Diez B. Roggisch at Feb 3, 2005 at 3:56 pm
    def test(self,arg):
    return getattr(self, arg)

    --
    Regards,

    Diez B. Roggisch
  • Diez B. Roggisch at Feb 3, 2005 at 4:02 pm

    Diez B. Roggisch wrote:

    def test(self,arg):
    return getattr(self, arg)
    Oops, missed the calling:

    def test(self,arg):
    return getattr(self, arg)()


    If you have more args, do this:

    def test(self, *args):
    return getattr(self, args[0])(*args[1:])


    --
    Regards,

    Diez B. Roggisch
  • Duncan Booth at Feb 3, 2005 at 4:11 pm

    Diez B. Roggisch wrote:

    If you have more args, do this:

    def test(self, *args):
    return getattr(self, args[0])(*args[1:])
    This would be cleaner written as:

    def test(self, method, *args):
    return getattr(self, method)(*args)

    and for complete generality:

    def test(self, method, *args, **kw):
    return getattr(self, method)(*args, **kw)
  • Diez B. Roggisch at Feb 3, 2005 at 8:30 pm

    def test(self, method, *args):
    return getattr(self, method)(*args)
    Yup, forgot abount that.

    --
    Regards,

    Diez B. Roggisch
  • Jeremy Bowers at Feb 3, 2005 at 7:28 pm

    On Thu, 03 Feb 2005 15:48:05 +0000, C Gillespie wrote:

    Dear All,

    I have a simple class
    class hello:
    def world(self):
    return 'hello'
    def test(self,arg):
    return self.arg

    When I want to do is:
    hello.test('world')
    'hello'

    i.e. pass the method name as an argument. How should I do this?
    In addition to Diez's point, I'd also point out you can do that from
    outside the class:

    Python 2.3.4 (#1, Jan 25 2005, 21:29:33)
    [GCC 3.4.3 (Gentoo Linux 3.4.3, ssp-3.4.3-0, pie-8.7.6.6)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    class Simple:
    ... def hello(self):
    ... print "hello"
    ...
    s = Simple()
    getattr(s, "hello")()
    hello
    >>>

    which may be more useful depending on what you are doing. I mention this
    because you are essentially re-implementing 'getattr' as a method of your
    object and it is likely you don't *really* want to do that, but I don't
    know, since we don't know what you are doing with this.
  • C Gillespie at Feb 4, 2005 at 9:23 am
    Dear All,

    Many thanks

    Colin
    "C Gillespie" <csgcsg39 at hotmail.com> wrote in message
    news:ctth3v$o2r$1 at ucsnew1.ncl.ac.uk...
    Dear All,

    I have a simple class
    class hello:
    def world(self):
    return 'hello'
    def test(self,arg):
    return self.arg

    When I want to do is:
    hello.test('world')
    'hello'

    i.e. pass the method name as an argument. How should I do this?

    Thanks

    Colin

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 3, '05 at 3:48p
activeFeb 4, '05 at 9:23a
posts7
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase