Hello,
Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();
Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?
Kind Regards,
Lukasz. 11 1427 lu********@gmail.com napisa³(a):
Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();
Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?
foo = getattr(module_or_object, 'function_name')
foo()
--
Jarek Zgoda
Skype: jzgoda | GTalk: zg***@jabber.aster.pl | voice: +48228430101
"We read Knuth so you don't have to." (Tim Peters)
On 19 Oct, 11:45, Jarek Zgoda <jzg...@o2.usun.plwrote:
lukasz....@gmail.com napisa³(a):
Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();
Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?
foo = getattr(module_or_object, 'function_name')
foo()
--
Jarek Zgoda
Skype: jzgoda | GTalk: zg...@jabber.aster.pl | voice: +48228430101
"We read Knuth so you don't have to." (Tim Peters)
Superb!
I was lookig for something like this. Belive me or not but i spent
lots of time looking for this simple solution :)
Cheers. lu********@gmail.com wrote:
On 19 Oct, 11:45, Jarek Zgoda <jzg...@o2.usun.plwrote:
>lukasz....@gmail.com napisa³(a):
Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();
Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?
foo = getattr(module_or_object, 'function_name') foo()
-- Jarek Zgoda Skype: jzgoda | GTalk: zg...@jabber.aster.pl | voice: +48228430101
"We read Knuth so you don't have to." (Tim Peters)
Superb!
I was lookig for something like this. Belive me or not but i spent
lots of time looking for this simple solution :)
The above clearly is a solution to your problem. I just wonder if you _need_
it. PHP doesn't have the concept of a function reference. So you need to
store/use names.
But in Python you can do this:
def foo():
print "foo"
callback = foo
callback()
As trivial this code is, it illustrates one thing: passing around functions
is as easy as passing around other values. So maybe you don't need a
function name. Only a suggestion to ponder about though, it might be that
your usecase is not suited for the above.
Diez
On Oct 19, 12:39 pm, lukasz....@gmail.com wrote:
Hello,
Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();
Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?
Kind Regards,
Lukasz.
Use apply(): http://docs.python.org/lib/non-essen...-in-funcs.html
Nils
Nils wrote:
On Oct 19, 12:39 pm, lukasz....@gmail.com wrote:
>Hello,
Is there any way (other then eval) to invoke a method by passing method name in a string. It's very simple in php: $oFoo = new Foo(); $dynamiMethod = "bar"; $oFoo->$dynamiMethod();
Unfortunately I can't find a good solution to do the same thing in python. Does it have some build-in function to do it?
Kind Regards,
Lukasz.
Use apply(): http://docs.python.org/lib/non-essen...-in-funcs.html
Apply doesn't help the OP - it's useful if you already have a function
pointer.
And it's obsoleted by the * and ** parameter passing mechanism.
Diez
Python will not allow string to be used a function pointer. It is type unsafe.
Best way is to convert string into function pointers manually.
if dynamicMethod == 'bar':
method = oFoo->bar
else:
method = oFoo->default
method()
On Friday 19 October 2007 7:56 am, Diez B. Roggisch wrote:
Nils wrote:
On Oct 19, 12:39 pm, lukasz....@gmail.com wrote:
Hello,
Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();
Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?
Kind Regards,
Lukasz.
Use apply(): http://docs.python.org/lib/non-essen...-in-funcs.html
Apply doesn't help the OP - it's useful if you already have a function
pointer.
And it's obsoleted by the * and ** parameter passing mechanism.
Diez
Sushant wrote:
Python will not allow string to be used a function pointer. It is type
unsafe. Best way is to convert string into function pointers manually.
if dynamicMethod == 'bar':
method = oFoo->bar
else:
method = oFoo->default
method()
Sorry to say so, but that answer is bogus. It's not even Python!
Jarek has already shown how to solve this problem. And type-safety-issues
have nothing to do with it at all.
Diez
On Fri, 19 Oct 2007 11:34:39 +0000, Nils wrote:
Use apply(): http://docs.python.org/lib/non-essen...-in-funcs.html
No, don't use apply. Not only does it not solve the original poster's
problem, it is a deprecated function. You shouldn't use it in new code.
--
Steven.
On Fri, 19 Oct 2007 10:29:12 -0400, Sushant wrote:
Python will not allow string to be used a function pointer. It is type
unsafe. Best way is to convert string into function pointers manually.
if dynamicMethod == 'bar':
method = oFoo->bar
else:
method = oFoo->default
method()
I don't know what language you're programming in there, but it isn't
Python.
method = oFoo->bar
^
SyntaxError: invalid syntax
--
Steven.
I did not know about getattr and it is the right thing.
getattr seems to be converting string into function pointer and I am just
saying that string cannot be used as a function pointer in Python as may be
in PHP.
I copied the PHP code so I did not replace arrow with dot. Good point :)
-Sushant.
On Friday 19 October 2007 11:05 am, Diez B. Roggisch wrote:
Sushant wrote:
Python will not allow string to be used a function pointer. It is type
unsafe. Best way is to convert string into function pointers manually.
if dynamicMethod == 'bar':
method = oFoo->bar
else:
method = oFoo->default
method()
Sorry to say so, but that answer is bogus. It's not even Python!
Jarek has already shown how to solve this problem. And type-safety-issues
have nothing to do with it at all.
Diez
Sushant napisa³(a):
getattr seems to be converting string into function pointer and I am just
saying that string cannot be used as a function pointer in Python as may be
in PHP.
It seems, but it does not. Getattr performs lookup on object's
attributes dict, it does not "convert" anything. The abstraction of
"function pointer" is also wrong here, it's a reference to an object
(any object, not just function). The result might seems similar, but
works quite differently.
--
Jarek Zgoda
Skype: jzgoda | GTalk: zg***@jabber.aster.pl | voice: +48228430101
"We read Knuth so you don't have to." (Tim Peters) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ogled |
last post by:
Hi
I have an odd question: is it possible to have a delegate that recieves
a methodname dynamically?
Like this: aClassObj.OnGo += new ANamespace.Invoke(fName);
where fName would be a string...
|
by: raca |
last post by:
I am trying to create a generic SOA ServiceInvoker that will accept an
XML string that will be used to deserialize an object generated by
XSDObjectGen. The hierarchy goes like this:...
|
by: Stephen Gennard |
last post by:
Hello,
I having a problem dynamically invoking a static method that takes a
reference to a SByte*. If I do it directly it works just fine.
Anyone any ideas why?
I have include a example...
|
by: Tamir Khason |
last post by:
Is it possible (as was in VB - CallByName) to call function which name was
generated.
Example:
private static void DS_function()
{
}
private static void FD_function()
{
|
by: Yechezkal Gutfreund |
last post by:
I have two subclasses of SpriteModel (1) LocalSprite (2)Sprite
Both implement a method called .ToXml() which returns an XmlDocument. But
they are different.
I instances of these objects...
|
by: Luis Arvayo |
last post by:
Hi,
In c#, I need to dynamically create types at runtime that will consist of
the following:
- inherits from a given interface
- will have a constructor with an int argument
|
by: pjr |
last post by:
Using VS2005, I dynamically create an event delegate. Code follows question.
My method gets the event's parameters and passes them onto a common event handler. My delegate gets called when expected...
|
by: alexis.meilland |
last post by:
Hello,
Well, I have a problem with a dynamic debuging.
My program is winform program written in Managed c++.
I compile dynamically a dll in vb.net. It works well. To execute a
function in...
|
by: =?Utf-8?B?RXZlcnQ=?= |
last post by:
In my (Windows Forms) project I am using strongly typed datatables. Now I am
trying to convert my 'rowfilter/group by' query logic to Linq. Most queries I
can easily convert, but some I am having...
|
by: Aaron \Castironpi\ Brady |
last post by:
Hello all,
To me, this is a somewhat unintuitive behavior. I want to discuss the
parts of it I don't understand.
.... f= lambda: n
....
9
9
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |