Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In Python variable names point at values. <code>x=y</code> tells Python that the variable name <code>x</code> should point at the value that <code>y</code> is currently pointing at.</p> <p>When you change <code>y</code>, then the variable name <code>y</code> points at a new value, while the variable name <code>x</code> still points at the old value.</p> <p>You can not achieve what you want with plain variable names.</p> <p>I like KennyTM's suggestion to define <code>x</code> as a function since it makes explicit that the value of <code>x</code> requires running some code (the lookup of the value of y).</p> <p>However, if you want to maintain a uniform syntax (making all the constants accessible in the same way), then you could use a class with properties (attributes which call getter and setter functions):</p> <p>Constants.py:</p> <pre><code>class BunchOConstants(object): def __init__(self, **kwds): self.__dict__.update(kwds) @property def x(self): return self.y @x.setter def x(self,val): self.y=val const=BunchOConstants(y=10,z='foo') </code></pre> <p>Your script.py:</p> <pre><code>import Constants const=Constants.const print(const.y) # 10 print(const.x) # 10 </code></pre> <p>Here you change the "constant" y:</p> <pre><code>const.y='bar' </code></pre> <p>And the "constant" x is changed too:</p> <pre><code>print(const.x) # bar </code></pre> <p>You can change <code>x</code> also,</p> <pre><code>const.x='foo' </code></pre> <p>and <code>y</code> too gets changed:</p> <pre><code>print(const.y) # foo </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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