0

for example, i would like my code to be:

name_of_function = input("Please enter a name for the function: ")
def name_of_function():
    print("blah blah blah")

which would work as:

Please enter a name for the function: hello
>>>hello()
blah blah blah
martineau
  • 119,623
  • 25
  • 170
  • 301
Prism
  • 1
  • 1
  • 2
    There are many ways to do it, but all of them scream "CODE SMELL!". – cs95 Oct 28 '17 at 23:54
  • Otherwise you can call a specific function for a specific user input – Simon Mengong Oct 28 '17 at 23:57
  • 1
    This Is one of those times where if you have to ask, you shouldn’t do it. It can be done, but there is almost never a time when it’s a good idea. What is the bigger problem you are trying to solve? – Bryan Oakley Oct 28 '17 at 23:58
  • right then ill tell you what i need it for: im making a discord bot in python and if i want to make a custom-commands command, i have to make the user be able to name the function – Prism Oct 29 '17 at 00:05
  • 1
    Possible duplicate of [How to convert a string to a function in python?](https://stackoverflow.com/questions/7719466/how-to-convert-a-string-to-a-function-in-python) – tommy.carstensen Oct 29 '17 at 00:10
  • This is not a good idea it will make your system security in danger – sasuri Oct 29 '17 at 00:36
  • Prism: Will you ever need to write code calling the function by its assigned name, or would it only be the user doing it? Also what code goes into the function and where does it come from? – martineau Oct 29 '17 at 00:40

4 Answers4

2

I would use a dictionary containing references to each function:

def func_one():
    print("hello")

def func_two():
    print("goodbye")

def rename_function(func_dict, orig_name, new_name):
    func_dict[new_name] = func_dict[orig_name]
    del func_dict[orig_name]

functions = {
    "placeholder_one": func_one,
    "placeholder_two": func_two
}

rename_function(
    functions,
    "placeholder_one",
    input("Enter new greeting function name: ")
)

rename_function(
    functions,
    "placeholder_two",
    input("Enter new farewell function name: ")
)

while True:
    func_name = input("Enter function to call: ")
    if func_name in functions:
        functions[func_name]()
    else:
        print("That function doesn't exist!")

Usage:

>>> Enter new greeting function name: hello
>>> Enter new farewell function name: goodbye
>>> Enter function to call: hello
hello
>>> Enter function to call: goodbye
goodbye
>>> Enter function to call: hi
That function doesn't exist!
Nick is tired
  • 6,860
  • 20
  • 39
  • 51
1
def hello():
    print('Hello!')

fn_name = input('fn name: ') # input hello
eval(fn_name)() # this will call the hello function

Warning: typically this isn't good practice, but this is one way of doing what you ask.

Onel Harrison
  • 1,244
  • 12
  • 14
0

You can, but you really shouldn't: This raises about a hundred and one odd concerns and potential issues. However, if you insist, the implementation would look something like the following:

def define_function(scope):

    name_of_function = input("Enter a name for the function: ")

    function_body = """def {}():

                        print("Blah blah blah.")


                    """.format(name_of_function)

    exec(function_body, scope)

Where from the Python shell, if you import the file containing this function (in my case, sandbox.py) and pass globals() or locals() to it, you can very tentatively get the interface you want.

>>> from sandbox import *
>>> define_function(globals())
Enter a name for the function: hello
>>> hello()
Blah blah blah.
Erick Shepherd
  • 1,403
  • 10
  • 19
0
class Foo(object):
    def foo1(self):
        print ('call foo1')

    def foo2(self):
        print ('call foo2')

    def bar(self):
        print ('no function named this')


def run(func_name):
    funcs = Foo()
    try:
        func = getattr(funcs, func_name)
    except Exception as ex:
        funcs.bar()
        return
    func()


func_name = raw_input('please input a function name:')
run(func_name)

Usage:

please input a function name:foo1
call foo1

please input a function name:foo3
no function named this

QuantumEnergy
  • 396
  • 1
  • 14