473,387 Members | 1,515 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,387 software developers and data experts.

generating method names 'dynamically'

Is it possible to have method names of a class generated somehow dynamically?

More precisely, at the time of writing a program that contains a class
definition I don't know what the names of its callable methods should
be. I have entries stored in a database that are changing from time to
time and I always want to be able to call (from another program) those
method names which are at the moment present in the database.
Jan 26 '06 #1
4 1435
> Is it possible to have method names of a class generated somehow dynamically?

If you don't know the name of the method ahead of time, how do you write
the code for it? Do you use some dummy name? If so, once you have the
name determined then you could use setattr to set the method name.
Here's a simple example:

class MyClass:
def DummyMethodName(self): pass

setattr(MyClass,'NewMethodName',MyClass.DummyMetho dName)

-Farshid
Jan 26 '06 #2
import inspect

x = ABC() # create an instance of class ABC
print inspect.getmembers(x,inspect.ismethod)
--------------------

Most of any introspection stuff can be done using the module "inspect".

Jan 27 '06 #3
x.__class__.__dict__[mname](x,*args,**kwargs)

here
x is an instance of a class FOO
FOO has a method "bar" (if the value of mname is "bar")
args is a tuple whose length is the number of positional arguments
accepted by bar
kwargs is a dictionary corresponding to the keyword arguments accepted
by bar.

Hope this answers your questions.

- Murali

Jan 27 '06 #4
Daniel Nogradi wrote:
Is it possible to have method names of a class generated somehow dynamically?

class Dummy(object): .... pass
.... def mymethod(self, *args, **kw): .... pass
.... setattr(Dummy, 'a_dynamically_generated_method_name', mymethod)

Dummy.a_dynamically_generated_method_name <unbound method Dummy.mymethod>

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jan 27 '06 #5

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

Similar topics

1
by: natkhatbandar | last post by:
Hi, I am generating an XML document dynamically using DOM. I am creating an element using IXMLDOMDocument::createElement() and then calling IXMLDOMElement::appendChild() for the parent element....
7
by: Venus | last post by:
Hello, I am trying to generate a dynamic form at runtime and would like to do it using "<asp: ..." form elements as follows Build up the string that is placed somewhere in the HTML code the...
9
by: hari krishna | last post by:
hi, I want to send the data from dataset information to Excel through ASP.Net. there will be no XL installed on web server. web server is win 2000 server machine. I am using visual basic code in...
13
by: vasudevmukherjee | last post by:
Hi! Can somebody help tell me why the following code gives a garbage value while producing first student's name, whereas it gives the names correctly for other three students - I really fail to...
0
by: krish | last post by:
hi, I am dynamically generating Imagebutton (say 5 imagebutton, in loop) . for each imagebutton url , i am dynamically generating image and assigining to it. every thing is ok upto this...
3
by: deciacco | last post by:
I'm trying to write a label printing SDI app with a small preview on the main form itself. Every time I run the InvalidatePreview event on the preview control to redraw the preview I get the...
0
by: Aswanth | last post by:
I'm Working with Asp.Net with C#.. & I'm Generating Reports in SSRS-2005.. Till Now I'm Generating Reports in SSRS-2005 with Stored Procedure.. in Which I'm Generating Reports for One...
7
Frinavale
by: Frinavale | last post by:
Is there a way to dynamically retrieve the names of the public methods for an Object?
0
by: TeenaRoz | last post by:
Hi, I am using axis jars to convert my java classes into wsdl for API purpose. I notice that, instead of generating the parameter names same as what my method accepts, I am getting it as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.