title: Dynamic Object Naming and Class Calling in Python
body: <p>I'm developing a programming language in Python where you can program a simulation of simple machines. I have written a function that takes some input, parses it, and finds out what the first word is.</p>
  
  <p>Now, for the first word insert, I need to take the next words <code>obj</code>, <code>name</code>, <code>x</code>, and <code>y</code>.</p>
  
  <blockquote>
    <p><strong><code>obj</code></strong>: what type of simple machine it is<br>
    <strong><code>name</code></strong>: what you want to call the object<br>
    <strong><code>x</code></strong>: X coordinate on the graph<br>
    <strong><code>y</code></strong>: Y coordinate on the graph</p>
  </blockquote>
  
  <p>I have already made a function <code>nextword</code> that iterates through the rest of the code and defines each variable as those words, so with the following code:</p>
  
  <pre><code>insert pulley JohnThePulley 3 4
  </code></pre>
  
  <p>It sees first word is <code>insert</code>, and calls my <code>insert</code> function.<br>
  Then, it sets <code>obj</code> to <code>pulley</code>, <code>name</code> to <code>JohnThePulley</code>, and so on. </p>
  
  <p>However, now I need to make an object in the daughter class <code>pulley</code>, under the mother class <code>simple_machines</code>, that has the name <code>JohnThePulley</code>, etc.</p>
  
  <p>The situation I'm in is that for the first word insert, for example, I don't know at all what the next word will be, from all the choices of daughter classes that they can call. I need to create the specified object along with the provided name, the provided X coordinate and the provided Y coordinate.</p>
  
  <p>I have tried doing simple formatting in python using <code>'{}'.format(name)</code> or <code>.format(obj)</code>, but those don't work.</p>
  
  <pre><code># Insert function
  def insert(code):
      c = 4
      syntax = np.array([obj, name, x, y])
      nextword(parser.code_array, syntax, c)
      objc += 1
      return
  
  
  # Nextword function, code_array[0] is insert, syntax is an array that
  # contains all the variables that need to be defined for any function
  def nextword(code_array, syntax, c):
      assert len(code_array) == c + 1, "Too Many Words!"
      for m in range(0, c):
          syntax[m] = code_array[m + 1]
      return
  
  
  # Mother Class simple_machines with properties
  class simple_machines:
      def __init__(self, obj, name, x, y, coords):
          self.obj = (
              obj
          )  # what type of obj, in this case, pulley
          self.name = name  # name, JohnThePulley
          self.x = x  # 3 in this case
          self.y = y  # 4 in this case
          self.coords = (x, y)  # (3,4) in this case
          return
  
  
  # Pulley Class, here so I can later define special properties for a pulley
  class pulley(simple_machines):
      def __init__(self, name, x, y):
          super(simple_machines, self).__init__()
          return
  
  
  # Code that I tried
  def insert(code):
      c = 4
      syntax = np.array([obj, name, x, y])
      nextword(parser.code_array, syntax, c)
      "{}".format(name) = "{}".format(obj)(
          name, x, y
      )  # this is what my
      # instantiation would look like, formatting an object with name, then
      # calling a class formatted with obj, and inserting their input of
      # name,x,y as the properties
      return
  </code></pre>
  
  <p>I expect an object in <code>pulley</code> to be created with the name <code>JohnThePulley</code>, and the coordinates X = 3 and Y = 4. What I'd like to result in, in simpler terms, is an object called <code>name</code> in a class called <code>obj</code> with the attributes <code>name.x</code>, <code>name.y</code>, etc</p>
  
  <p>However, I get errors like:</p>
  
  <pre><code>NameError: name 'obj' is not defined
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>SyntaxError: can't assign to function call
  </code></pre>
  
  <p>The first one apparently means that the word <code>obj</code> isn't being assigned, but the second one apparently means that I can't format a function name or format a variable name and define it as a function (even though I'm instantiating it as a class).</p>
  
  <p>What am I doing wrong? How can I fix this?</p>