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
>
> 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
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
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, *
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
-
def test(self,arg):
return getattr(self, arg)
--
Regards,
Diez B. Roggisch
--
http://mail.python.org/mailman/listinfo/python-list
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/