Note that there are some explanatory texts on larger screens.

plurals
  1. POClass scope vs module scope attributes in Python
    primarykey
    data
    text
    <p>I declare a class that does some computations using several hard-coded constants that are defined as class attributes. All of the methods look similar to the following:</p> <pre><code>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 </code></pre> <p>The class scope qualifiers makes the code (even more) difficult to read. I had thought of doing something like this to make the code more readable:</p> <pre><code>... _ = IAPWS_1995 _dAr_ddelta = \ sum(_.n_0 * _.d_0 * pow(delta, _.d_0-1) * pow(tau, _.t_0)) + \ ... </code></pre> <p>If I move the constant declarations to the module scope, though, I don't need scope qualifiers at all. </p> <p>Is there a reason to prefer declaring the constants in the class over the module (e.g. namespace collisions if I have a similar class <code>IAPWS_2014</code> in the future?)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload