Before I started writing Python code, I spent most of my time working with PHP. There are a few things that PHP makes first class citizens to the language. One of them is dynamic class inclusion, and the other is the ability to dynamically call a function. Show Python on the other hand does not make either of these easy. There is good reason. Both autoloading classes and dynamically executing functions lean toward the magical side of programming. Code that uses dynamic function calls can become confusing very fast.
What is a Dynamic Function Call?You may be wondering what I mean by "dynamically" call a function. Let's look at a quick example in PHP. Even if you have never looked at PHP, you should be able to understand what is going on.
Note: in this example the type declarations are optional, but they are a good practice. In this example, the function 0. As long as we know the name of the function we want to call, we can execute the function and even pass in arguments.This is the idea behind dynamic function execution. The function that will be executed is not determined when the code is written. It is determined at runtime. Doing this in Python is a little more complicated. As we said earlier, that is not necessarily bad. To do this in Python you have to access the global namespace. Python makes you do this explicitly where PHP is implicit.
In this example we use the 1 function to access the global namespace. 1 returns a dictionary that includes 0 as a key and the value is a reference to the area() function. If you are not familiar with 1 a simple change to our code can give you a clear idea of what we are doing.
You can see that both 0 and 7 point to the same address in memory 8. This means that in the current scope calling 9 and 0 would run the exact same function.Don't Abuse GlobalsJust because you can do something does not mean you should. Using 1 in this way is often frowned on as neither pythonic nor safe. It is safer to use 2 or 3 to access just the local scope, but still not ideal.Fortunately, the objects in scope in this way is not as dangerous as it could be. Python built in functions and classes are available in the 4 key. This is very important, since It means calling 5 will raise a 6. You would actually need to call 7.Let's be honest Python's built in functions and classes are not the only exploitable objects in any program. A better way is to use a class to encapsulate the methods you want to make available to execute at runtime. You can create each function as a method of the class. When the name of a function is provided you can test to see if it is an attribute of the class and callable.
If you are looking at the 8 statement in the 9 method and getting a little confused. Don't worry. I am using some syntax that is new in Python 3.8.The assignment expression 0 allows you to both set the value of a variable and evaluate the result of an expression in a single line.The function is equivalent to the following...
The difference is that 1 does not need to be evaluated twice.Let's get back to our example. The 9 method is really doing most of the heavy lifting here. It first takes the 3 and appends 4 to the front. This is often a good idea. Your class will likely have few extra methods and attributes that you don't want to expose.Once the name of the function has been determined we can check to make sure it is a valid attribute and a function that we can call. The last thing we do is simply call the function. Dynamic Function ArgumentsPassing arguments to the dynamic function is straight forward. We simply can make 9 accept 6 and 7 then pass that to 8.
Of course, you will need to handle the arguments in the function that will be called. Currently, none of the 9 methods in our example accept arguments.One of the complexities of using arguments with dynamic function execution is the need for each function to handle the same arguments. This requires you to standardize your function arguments. Dynamic Function Uses in PythonTwo of the common uses for dynamic function execution in PHP are callbacks and hooks. However, in Python neither of these are very common. I think Python's focus on being explicit keeps developers from moving toward meta and magical programming. Python also provides decorators that can be used to explicitly hook or wrap a function. These two things limit the use of dynamic function calls. I think the best example of dynamic function execution in Python is form validation in Django. Let's say we had a form like this...
When Django validates and parses the values to native Python data types it will call a function for each field. The function is 0 where 1 is the name of the field.In this example we could add a method to ensure the value of name is valid and formatted the way we want.
This is a very simple example. We are just taking whatever value is given in 2 and making it lower case. 3 is called by Django's 4 method.The 5 method from Django is a good example of how to execute a function dynamically.
The reason 5 exists is not because it is imposible to do this any other way. But because it provides a clean interface for developers building forms in Django.I think this is a good example of why you may want to use this pattern. In general, I would recommend avoiding it, but there are times when it makes a library or API easy for developers to use. In those instances, don't shy away from doing it, but make sure you do it safely! How do I get a class name dynamically?To use the feature, you will need to enable the setting to allow Dynamic Class Names under SETTINGS>SETUP>CLASS SETTINGS>GENERAL CLASS SETTINGS>"Enable Dynamic Class Names" and then configure your Dynamic Class Name Pattern.
How to call a class method dynamically in Java?The Class object, representing the type in which the method is defined, provides two ways of doing this.. 3.1. getMethod() We can use getMethod() to find any public method of the class or any of its superclasses. ... . 3.2. getDeclaredMethod() We can use getDeclaredMethod() to get any kind of method.. How to call a class dynamically in C#?string methodName = "hello"; //Get the method information using the method info class MethodInfo mi = this. GetType(). GetMethod(methodName); //Invoke the method // (null- no parameter for the method call // or you can pass the array of parameters...)
How to call a class dynamically in PHP?#Using the Callable Array Syntax
You can use the callable array syntax, where you specify the object instance or class name at index 0 and the method name at index 1 (regardless of whether it's a static method or a non-static one). For example: // PHP 5.5+ $callable = [$obj, $nonStaticMethod]; $callable();
|