Class scope vs module scope attributes in Python -
i declare class computations using several hard-coded constants defined class attributes. of methods similar following:
class iapws_1995: @staticmethod def dar_ddelta(delta, tau, delta, theta, psi): _dar_ddelta = \ sum(iapws_1995.n_0 * iapws_1995.d_0 * pow(delta, iapws_1995.d_0-1) * pow(tau, iapws_1995.t_0)) + \ sum(iapws_1995.n_1 * exp(-pow(delta, iapws_1995.c_1)) * (pow(delta, iapws_1995.d_1-1) * pow(tau, iapws_1995.t_1) * (iapws_1995.d_1 - iapws_1995.c_1*pow(delta, iapws_1995.c_1)))) + \ sum(iapws_1995.n_2 * pow(delta, iapws_1995.d_2)*pow(tau, iapws_1995.t_2) * exp(-iapws_1995.alpha_2*(delta-iapws_1995.epsilon_2)**2 - iapws_1995.beta_2*(tau-iapws_1995.gamma_2)**2) * (iapws_1995.d_2/delta - 2*iapws_1995.alpha_2*(delta-iapws_1995.epsilon_2))) + \ sum(iapws_1995.n_3 * (pow(delta, iapws_1995.b_3)*(psi + delta*iapws_1995.dpsi_ddelta(psi, delta)) + iapws_1995.ddeltab_ddelta(delta, delta, theta)*delta*psi)) return _dar_ddelta
the class scope qualifiers makes code (even more) difficult read. had thought of doing make code more readable:
... _ = iapws_1995 _dar_ddelta = \ sum(_.n_0 * _.d_0 * pow(delta, _.d_0-1) * pow(tau, _.t_0)) + \ ...
if move constant declarations module scope, though, don't need scope qualifiers @ all.
is there reason prefer declaring constants in class on module (e.g. namespace collisions if have similar class iapws_2014
in future?)
another approach create module called iapws_1995
, since aren't instantiating objects class. module like:
n_0, n_1, n_2, n_3 = ... d_0, d_1, d_2 = ... t_0, t_1, t_2 = ... c_1, = ... b_3, = ... alpha_2, beta_2, gamma_2, epsilon_2 = ... dpsi_ddelta = ... ddeltab_ddelta = ... def dar_ddelta(delta, tau, delta, theta, psi): _dar_ddelta = ( sum(n_0 * d_0 * pow(delta, d_0-1) * pow(tau, t_0)) + sum(n_1 * exp(-pow(delta, c_1)) * (pow(delta, d_1-1) * pow(tau, t_1) * (d_1 - c_1*pow(delta, c_1)))) + sum(n_2 * pow(delta, d_2)*pow(tau, t_2) * exp(-alpha_2*(delta-epsilon_2)**2 - beta_2*(tau-gamma_2)**2) * (d_2/delta - 2*alpha_2*(delta-epsilon_2))) + \ sum(n_3 * (pow(delta, b_3)*(psi + delta*dpsi_ddelta(psi, delta)) + ddeltab_ddelta(delta, delta, theta)*delta*psi))) return _dar_ddelta
with constants module-level variables, , class method module-level function.
other code use this:
import iapw2_1995 x = iapw2_1995.dar_ddelta( arg1, arg2, arg3, arg4, arg5 )
Comments
Post a Comment