Re: Calling a method using an argument

2005-02-04 Thread C Gillespie
Dear All, Many thanks Colin "C Gillespie" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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('w

Re: Calling a method using an argument

2005-02-03 Thread Diez B. Roggisch
> > def test(self, method, *args): > return getattr(self, method)(*args) Yup, forgot abount that. -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list

Re: Calling a method using an argument

2005-02-03 Thread Jeremy Bowers
On Thu, 03 Feb 2005 15:48:05 +, 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

Re: Calling a method using an argument

2005-02-03 Thread Duncan Booth
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, *

Re: Calling a method using an argument

2005-02-03 Thread Diez B. Roggisch
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 -

Re: Calling a method using an argument

2005-02-03 Thread Diez B. Roggisch
def test(self,arg): return getattr(self, arg) -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list

Calling a method using an argument

2005-02-03 Thread C Gillespie
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 -- http://mail.python.org/