PHP » PYTHON |
login |
register |
about
|
PYTHON function_exists
is this article helpful?
|
Python replacement for PHP's function_exists
[
edit
| history
]
This snipplet only works if you want to check if a function exists in your current module, but not inside os.path or so:
funcName = "MyFunction" try: func = locals()[ funcName ] if callable( func ): func( argumentA, argumentB ) # depends on function, of course except: print funcName, "is not defined" # please note I'm a beginner, don't punish me if it is not the most beautiful way ;-) An actual replacement rather than a snippet: import types def function_exists(fun): '''As in PHP, fun is tested as a name, not an object as is common in Python.''' try: ret = type(eval(str(fun)) return ret in (types.FunctionType, types.BuiltinFunctionType) except NameError: return False function_exists(PHP 4, PHP 5) function_exists — Return TRUE if the given function has been defined Description
bool function_exists
( string $function_name
)
Checks the list of defined functions, both built-in (internal) and user-defined, for function_name . Parameters
Return ValuesReturns TRUE if function_name exists and is a function, FALSE otherwise.
Examples
Example #1 function_exists() example
<?php
Notes
See Also
|
more
Recently updated
more
Most requested
more
Last requests
|