473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

how to access class methods via their name-as-string

I'd like to be able to look up a method name by passing a string with
the method name. I thought I could use self.__dict__ to get at the
method names, but as my example shows, this is obviously not working.
I've also tried self.__class__.__dict__ without success.

Can anyone point me in the right direction to make this work?

Thanks,
Phil
class a: def m1(self):
print 'm1'
def __getitem__(self, k):
return self.__dict__[k]
class b(a): def m2(self):
print 'm2'

z=b()
dir(z) ['__doc__', '__getitem__', '__module__', 'm1', 'm2'] z['m1'] Traceback (most recent call last):
File "<pyshell#56>", line 1, in ?
z['m1']
File "<pyshell#51>", line 5, in __getitem__
return self.__dict__[k]
KeyError: 'm1'


Jul 18 '05 #1
1 1341
ph*****************@yahoo.com wrote:
I'd like to be able to look up a method name by passing a string with
the method name.


Use getattr:

py> class A(object):
.... def f(self):
.... pass
.... def g(self):
.... pass
....
py> class B(A):
.... def h(self):
.... pass
....
py> getattr(B(), 'f')
<bound method B.f of <__main__.B object at 0x011D7790>>
py> getattr(B(), 'g')
<bound method B.g of <__main__.B object at 0x01222390>>
py> getattr(B(), 'h')
<bound method B.h of <__main__.B object at 0x011D7790>>

Steve
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fernando Rodriguez | last post by:
Hi, I have a base class with a 'sanity-check' method. This method should iterate through all the methods and check if they are all 'thunks' (zero parameter functions, well actually, 1 parameter:...
4
by: Maarten van Reeuwijk | last post by:
Hello, Maybe I was a little too detailed in my previous post . I can boil down my problem to this: say I have a class A that I encapsulate with a class Proxy. Now I just want to override and add...
3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
0
by: BerkshireGuy | last post by:
Hello, I am trying to close the MsQuery window from within an Access 2000 module. On Dev Ashish's site, he has code to find the class name and then another function to close an external...
1
by: Davids | last post by:
ok this is basic but it really puzzles me. I have an .aspx page and within it a user control (for example mycontrol.aspx and it's code file mycontrol.aspx.cs). In the code for my base page I...
3
by: DwC | last post by:
Hi, We have a ms access database that we will be using to develop a website which would have fairly low usage levels. We have some experience with windows apps but not so much with asp.net...
10
by: Ronnie | last post by:
I've created a simple web application using VS2005 Beta 2. Basically, I've added a Web Form and a Global.asax file. In my Global.asax, I create a method like: public static void TestGlobal()...
3
by: Mark Gilkes | last post by:
I would just like to get a feeling for what others are doing here. Having read through the 'Data Tiers' paper under MS's Paterns and Practices I am designing and developing a small(ish) 3 layered...
11
by: glen.coates.bigworld | last post by:
I'm developing a library at the moment that involves many classes, some of which have "exposed" capabilities. I'm trying to design a nice interface for both exposing those capabilities, and...
0
by: Gabriel Genellina | last post by:
En Tue, 29 Jul 2008 01:17:13 -0300, Piyush Anonymous <piyush.subscription@gmail.comescribi�: You forget the self argument (this explains the error you got). It's a lot easier than that;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.