Questions tagged [dynamic-function]
75 questions
347
votes
18 answers
How to call a function from a string stored in a variable?
I need to be able to call a function, but the function name is stored in a variable, is this possible? e.g:
function foo ()
{
//code here
}
function bar ()
{
//code here
}
$functionName = "foo";
// I need to call the function based on what…

Lizard
- 43,732
- 39
- 106
- 167
63
votes
7 answers
Python dynamic function creation with custom names
Apologies if this question has already been raised and answered.
What I need to do is very simple in concept, but unfortunately I have not been able to find an answer for it online.
I need to create dynamic functions in Python (Python2.7) with…

mahdiolfat
- 821
- 1
- 9
- 12
20
votes
3 answers
How to define a dynamic mixin or function name in SASS?
I want to dynamically create mixins in SASS, named after each item in the list, but it doesn't seem to work.
I tried this but I get an error:
$event-icons: fair, concert, art-show, conference, dance-show, film, party, festival, theatre,…

Cristian
- 5,877
- 6
- 46
- 55
9
votes
4 answers
__callStatic(), call_user_func_array(), references, and PHP 5.3.1
I've been reading around on SO, and elsewhere, however I can't seem to find anything conclusive.
Is there any way to effectively carry references through this call stack, resulting in the desired functionality as described in the example below?…

Dan Lugg
- 20,192
- 19
- 110
- 174
8
votes
1 answer
Scala - Creating a basic dynamic function parser
I'm new to Scala but I was wondering it it is possible to implement a simple Equation parser in the language.
Say I have a few functions (much like Excel functions):
IF(Cond a=b, val_true, val_false)
MID(String, Start_pos, num_chars) - string…

NightWolf
- 7,694
- 9
- 74
- 121
8
votes
3 answers
Using C++ with assembly to allocate and create new functions at runtime
I've been working on a (C++) project, which requires completely dynamically allocated functions, which means malloc/new and mprotect and then modify the buffer manually to assembly code. Because of this I've wondered exactly, what is required in…

Elliott Darfink
- 1,153
- 14
- 34
7
votes
2 answers
Dynamic function creation from another function
I have a Fortran 90 subroutine which takes a function as an argument, and I would like to pass a modified version of that function into another subroutine. I want the program to look something like this:
subroutine foo(f, ...)
real …

Eric Langlois
- 572
- 4
- 12
7
votes
2 answers
Python setattr() to function takes initial function name
I do understand how setattr() works in python, but my question is when i try to dynamically set an attribute and give it an unbound function as a value, so the attribute is a callable, the attribute ends up taking the name of the unbound function…

nosahama
- 150
- 10
7
votes
1 answer
Dynamic function name in Angular 2 click event
While this answer is probably dead simple, I seem to be stuck. As an Angular 2 beginner, I have tried all possible combinations of {}, [] and () brackets to achieve the following:
where:
this.action =…

dvdbrk
- 161
- 2
- 10
6
votes
3 answers
How do you create a callback function (dispatch table) in Perl using hashes?
I want to call a main controller function that dispatches other function dynamically, something like this:
package Controller;
my %callback_funcs = ();
sub register_callback{
my ($class,$callback,$options) = _@;
#apppend to %callback_funcs…

qodeninja
- 10,946
- 30
- 98
- 152
6
votes
2 answers
Dynamic function calling Angular 2
I have function name in a variable and I am assigning that variable on click event of button. but it is not working. Any Help?
@Component({
selector: 'my-app',
template: `
Function name is: {{FunctionName}}

amansoni211
- 889
- 2
- 13
- 30
5
votes
1 answer
Php - understanding create_function() - passing simple variable
First time I am trying to use the dynamic create_function, and up to now, not much success :-)
My function is this :
function o99_brsa_custom_widgets() {
global $wp_meta_boxes;
global $o99_brsa_options;
for($i=0; $i>…

Obmerk Kronen
- 15,619
- 16
- 66
- 105
5
votes
2 answers
python - how can I dynamically create a function with a name based on a string read in from a file?
Let's say I have a file containing the string "unpredictable_words". I would like to read in this string and then define a function as follows:
def test_unpredictable_words(self):
do_important_stuff()
I would then like to inject this function…

tadasajon
- 14,276
- 29
- 92
- 144
4
votes
1 answer
Pass a function with undetermined number of arguments and call it with the variadic arguments
I'd like to create a function "lazy" which accepts a function with an undetermined number of arguments as parameter. What type do I need or which casts have to be done?
Then I want to execute that thing later in function "evaluate". How do I then…

bwoebi
- 23,637
- 5
- 58
- 79
4
votes
1 answer
Python dynamic functions from strings
I came across few related answers but not what I want.
Here is the code I have now:
code_str = """
print "this is my global x = " + x
print "And another line is done"
"""
x = 'mystery'
func_template = lambda p: None
func_template.__code__ =…

Ayman
- 11,265
- 16
- 66
- 92