|
|
|
|
|
|
| Author |
Message |
C Gillespie *nix forums beginner
Joined: 24 May 2005
Posts: 5
|
Posted: Fri Feb 04, 2005 8:23 am Post subject:
Re: Calling a method using an argument
|
|
|
Dear All,
Many thanks
Colin
"C Gillespie" <csgcsg39@hotmail.com> wrote in message
news:ctth3v$o2r$1@ucsnew1.ncl.ac.uk...
| Quote: | 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
|
|
|
| Back to top |
|
 |
Diez B. Roggisch *nix forums Guru
Joined: 20 Feb 2005
Posts: 882
|
Posted: Thu Feb 03, 2005 7:30 pm Post subject:
Re: Calling a method using an argument
|
|
|
| Quote: |
def test(self, method, *args):
return getattr(self, method)(*args)
|
Yup, forgot abount that.
--
Regards,
Diez B. Roggisch |
|
| Back to top |
|
 |
Jeremy Bowers *nix forums Guru Wannabe
Joined: 23 Feb 2005
Posts: 129
|
Posted: Thu Feb 03, 2005 6:28 pm Post subject:
Re: Calling a method using an argument
|
|
|
On Thu, 03 Feb 2005 15:48:05 +0000, C Gillespie wrote:
| Quote: | 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.
| Quote: | class Simple:
.... def hello(self): |
.... print "hello"
....
| Quote: | 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. |
|
| Back to top |
|
 |
Duncan Booth *nix forums Guru
Joined: 11 Mar 2005
Posts: 422
|
Posted: Thu Feb 03, 2005 3:11 pm Post subject:
Re: Calling a method using an argument
|
|
|
Diez B. Roggisch wrote:
| Quote: | 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) |
|
| Back to top |
|
 |
Diez B. Roggisch *nix forums Guru
Joined: 20 Feb 2005
Posts: 882
|
Posted: Thu Feb 03, 2005 3:02 pm Post subject:
Re: Calling a method using an argument
|
|
|
Diez B. Roggisch wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Diez B. Roggisch *nix forums Guru
Joined: 20 Feb 2005
Posts: 882
|
Posted: Thu Feb 03, 2005 2:56 pm Post subject:
Re: Calling a method using an argument
|
|
|
def test(self,arg):
return getattr(self, arg)
--
Regards,
Diez B. Roggisch |
|
| Back to top |
|
 |
C Gillespie *nix forums beginner
Joined: 24 May 2005
Posts: 5
|
Posted: Thu Feb 03, 2005 2:48 pm Post subject:
Calling a method using an argument
|
|
|
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:
| Quote: | hello.test('world')
'hello' |
i.e. pass the method name as an argument. How should I do this?
Thanks
Colin |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Jan 08, 2009 9:39 pm | All times are GMT
|
|
Mortgages | Mortgage insurance | MPAA | Looking for Credit Cards? | McDonalds
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|