On 2020-10-22 at 20:04:14 -0400, Michael Smith <mich...@smith-li.com> wrote:
> 1. Being able to call two different functions from the same module. (AND) > 2. Being able to call some functions that exist today without modification > or rearrangement. (AND) > 3. Being able to invoke these things from python without installing a > module from pip. > > There are solutions that do some of these, but nothing that does them all. > I think it's surprising that the python makers have found worthwhile three > different solutions for invoking modules, but this list so far has seen > little value in invoking a function. The following module meets your three criteria: %% begin Python code def f(): print("This is f.") def g(): print("This is g.") h = 'hello' i = lambda x: print(f"Hello, {x}.") if __name__ == '__main__': import sys try: function_name, *arguments = sys.argv[1:] except (IndexError, ValueError): print('missing function name') sys.exit(1) function = sys.modules[__name__].__dict__.get(function_name) if not function or not callable(function): print('cannot find or cannot call', function_name) sys.exit(1) function(*arguments) %% end Python code It's not polished, and it only handles positional string parameters, but it should be fairly self-explanatory. The __name__ == '__main__' idiom allows the module to be imported by other modules without incident. IMO, at best, it might be the beginning of a recipe somewhere rather than being included in the standard library. I agree with David Mertz that as soon as it gets more complex, every application is going to want something a little different. > It would definitely be more empowering to be able to describe the > command line args to such an invocation using the args to the > function. Such enhancements are left as an exercise to the interested reader. HTH, Dan _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/GZORVBOPBGFGCIWR2YCZXQ657JTUJ52Y/ Code of Conduct: http://python.org/psf/codeofconduct/