Archive for the 'Reflection' Category

Dynamically Invoke a Function in Python

In this post, I referred to an article that explains how to invoke methods dynamically (when you have the name of the method as a string object). Today I ran into a problem where I needed to do this, but the methods were not contained within a class. I just had them declared in my [...] Read more »

Dynamically Invoke a Function in R

One way the R programming language has been described is that it is a functional programming language. Whether it would be called this by purists, I don’t know. But part of what this means is that all functions are treated as objects. So you can pass functions around very easily. This might sound strange, but [...] Read more »

How to List All Methods in a Python Object

Suppose you have an object in Python that you retrieved from a third-party library, but you don’t have access to the source code or to very good documentation. Believe me, it happens (and did to me today). You can use a simple built-in method in Python to find out which methods are exposed by a [...] Read more »

How to Invoke a Method Dynamically in Python (Reflection)

Sometimes you want to be able to invoke a method dynamically rather than calling it explicitly. Or in other words, let’s say you have a bunch of methods, and you have a string object telling you which method to execute, but rather than doing a bunch of if statements you want to be able to [...] Read more »