title: How do I dynamically check and set an instances variable values?
body: <p>I want to be able to dynamically check and set an instances attribute values. The use-case is for a point-like class, where I want to have a custom 'set_value' method that checks for which value the client is trying to set, and then determines what to do from there.</p>
  
  <p>My goal is to have a method which allows the values (x, y) of the instance to be set once, and then disallows future assignments to those variables... effectively making them 'immutable'.
  I know that this does not technically make them immutable, I'm relying on the gentleman's agreement to not do funny stuff and mess around with it.</p>
  
  <p>I've trying messing around with the <strong>setattr</strong> method, to no avail, as it then won't allow the init to run in the first place.</p>
  
  <p>My class is shown below:</p>
  
  <pre><code>class Node:
      """Represents the nodes as points with a position x, y."""
  
      def __init__(self, x=0, y=0, x_state=0, y_state=0):
          """Create a new node at x, y"""
          self.x = x
          self.y = y
  
          if self.x != 0:
              self.x_state = 1
          else:
              self.x_state = x_state
  
          if self.y != 0:
              self.y_state = 1
          else:
              self.y_state = y_state
  
      # some other dunder methods here
  
      def set_value(self, attr, value):
          if self.[attr]_state == 0:
              self.[attr] = value
              self.[attr]_state = 1
          else:
              raise NotImplementedError
  </code></pre>
  
  <p>The method checks the appropriate 'state' variable and then assigns the x or y variable to the value given by the user if the 'state' is 0, and then gives the 'state' a value of 1, thereby rendering future value assignments useless.</p>
  
  <p>The confusion I have is with dynamically referencing the attributes.</p>
  
  <p>Is this possible?</p>
  
  <p>Thanks in advance for any assistance!</p>