niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
Calling a method using an argument
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
C Gillespie
*nix forums beginner


Joined: 24 May 2005
Posts: 5

PostPosted: Fri Feb 04, 2005 8:23 am    Post subject: Re: Calling a method using an argument Reply with quote

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

PostPosted: Thu Feb 03, 2005 7:30 pm    Post subject: Re: Calling a method using an argument Reply with quote

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

PostPosted: Thu Feb 03, 2005 6:28 pm    Post subject: Re: Calling a method using an argument Reply with quote

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

PostPosted: Thu Feb 03, 2005 3:11 pm    Post subject: Re: Calling a method using an argument Reply with quote

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

PostPosted: Thu Feb 03, 2005 3:02 pm    Post subject: Re: Calling a method using an argument Reply with quote

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

PostPosted: Thu Feb 03, 2005 2:56 pm    Post subject: Re: Calling a method using an argument Reply with quote

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

PostPosted: Thu Feb 03, 2005 2:48 pm    Post subject: Calling a method using an argument Reply with 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:
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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 9:39 pm | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Best method to search kristy420 Oracle 0 Tue Nov 18, 2008 6:34 pm
No new posts calling stored procedure using ECPG Jasbinder Bali PostgreSQL 1 Thu Jul 20, 2006 7:53 pm
No new posts access method "gin" does not exist Kevin Murphy PostgreSQL 0 Thu Jul 20, 2006 4:25 pm
No new posts Bug#379010: ITP: scim-thai -- ITP: scim-thai -- Thai Inpu... Theppitak Karoonboonyanan devel 0 Thu Jul 20, 2006 3:30 pm
No new posts Error while calling an Oracle Stored Procedure from VB us... macca Server 5 Thu Jul 20, 2006 9:32 am

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
[ Time: 0.1521s ][ Queries: 20 (0.0679s) ][ GZIP on - Debug on ]