0

I have the following code, where I'm using mpmath library for calculating different irrational numbers:

for i in range(1, len(self.layer_sizes) - 1):
          z = np.dot(a, self.weights[i])
          mp.dps = np.max(abs(z))+self.layer_windows[i] + 100
          irratio = eval('mp.' + self.irratio_values[i])

The purpose of this code is to calculate for each iteration the corresponding irrational number with the desired length from the max value of a vector, because each value of the vector will be used as an index for retrieving the value of the irrational number at that index, but also each layer has its corresponding irrational number, so for example, in the first layer it will be used pi as irrational number, but then the next layer will use sqrt(2). Although this code is already working, I heard that the use of eval is a bad practice of coding.

I'm expecting an alternative of using eval or if it is used right in this case.

  • Are you saying `self.irratio_values` contains a list of function names? You can use `getattr(mp, 'pi')` to fetch `mp.pi`. – Tim Roberts Jul 24 '23 at 05:37

0 Answers0