Pierpaolo
Pierpaolo

Reputation: 1741

automatic definition of new functions in python

I have some data stored in a object Foo().

Foo.data is a list of n x m np.array, where each array represent some experimental data at a specific time. The array is composed by m variables, and n correspond to the number of different point where measurement has been taken.

We can consider that we have at our disposal a list of variables name, e.g:

var_names=['x','volume','u','L_bar','rho','P',.....]

I want like to make data more accessible, defining something like:

def Foo.P(self,x,time)
    index_t=time_list.index(time)
    index_x=var_names.index(x)
    index_P=var_names.index(P)
    return Foo.data[index_t][index_x,index_P]

The question is: considering that the list of variables is not fixed,but it can vary, can I automatically define some functions that do what I show before without explicitly defining one for each variable?

I mean, I would like to have a bunch of function such as, Foo.P, Foo.volume, Foo.u, Foo.rho that extract the value of the variable given by the function name, for a (x,time) position, without defining all of them one by one.

Upvotes: 0

Views: 92

Answers (2)

John Zwinck
John Zwinck

Reputation: 249472

You can do it by defining the special __getattr__ method on Foo, like this:

def __getattr__(self, name):
    """If name is known, return a function to get that data for given (x, time)."""
    if name not in var_names: # name not known
        raise AttributeError
    return lambda x, time: self.data[time_list.index(time)][var_names.index(x), var_names.index(name)]

Upvotes: 2

Stephen Lin
Stephen Lin

Reputation: 4912

Why don't you just pass the name through? Like this:

#!/usr/bin/python
# -*- coding: utf-8 -*-


var_names=['x','volume','u','L_bar','rho','P']

def general_foo(self, name, x, time)
    index_t=time_list.index(time)
    index_x=var_names.index(x)
    index_P=var_names.index(name)
    return Foo.data[index_t][index_x,index_P]

for var in var_names:
    general_foo(var, x, time)

Upvotes: 1

Related Questions