title: Class instance implementation, initializing instance - from SICP python
body: <p>I'm trying to the understand the initialization function in a python class system implementation, taken from this book <a href="http://www-inst.eecs.berkeley.edu/~cs61a/sp12/book/objects.html#instances" rel="nofollow noreferrer">(SICP python - reference to book section)</a>. </p>
  
  <p>The <code>init_instance</code> (initialization) function <code>"""Return a new object with type cls, initialized with args."""</code> is where I'm having trouble. Below I've tried to narrow down my question, by explaining what I've understood.</p>
  
  <pre><code>def make_instance (cls): #good with this
      """return a new object instance, which is a dispatch dictionary"""
      def get_value(name):
          if name in attributes:
              return attributes[name]
          else:
              value = cls ['get'](name)
              return bind_method (value, instance)
      def set_value (name, value):
          attributes [name] = value
      attributes = {}
      instance = {'get': get_value, 'set': set_value}
      return instance
  
  def bind_method (value, instance): #good with this
      """Return a bound method if value is callable, or value otherwise"""
      if callable (value):
          def method(*args):
              return value (instance, *args)
          return method
      else:
          return value
  
  def make_class (attributes, base_class = None): 
      """Return a new class, which is a dispatch dictionary."""
      def get_value(name):
          if name in attributes:
              return attributes[name]
          elif base_class is not None:
              return base_class['get'](name)
      def set_value(name,value):
          attributes[name] = value
      def new(*args):
          return init_instance(cls, *args)
      cls = {'get':get_value,'set':set_value,'new':new}
      return cls
  
  def init_instance(cls,*args): #problem here
      """Return a new object with type cls, initialized with args"""
      instance = make_instance (cls)
      init = cls ['get'] ('__init__')
      if init:                            
          init (instance, *args)          #No return function here
      return instance
  </code></pre>
  
  <p>Here is a call to above functions, to create a new class object called 'Jim' </p>
  
  <pre><code>def make_my_class():    #define a custom class
      pass
      return make_class({'__init__':__init__})   #return function that implements class
  
  my_class = make_my_class()  #create a class
  my_class_instance = my_class['new'] ('Jim') #create a class instance with ['new']
  </code></pre>
  
  <h3>What I understand</h3>
  
  <p>Since this is an functional implementation of classes, the comparison is with inbuilt python classes. Below wherever I say Python Class/object/instance, I mean inbuilt.</p>
  
  <ul>
  <li><code>make_instande(cls)</code> : takes the 'class' -> <code>cls</code> argument (a message fxn dictionary itself) and describes the behaviour of an object , i.e. provides required properties to behave in a similar way to a python object. We can set attributes, that stay local to attributes dictionary using '<code>set</code>'. Using <code>get</code>, if the attribute is not in object, it is looked up in class definition and a <code>bind_method</code> function is called. </li>
  <li><code>bind_method(value,instance)</code>: binds a function in class definition to the object instance to emulate python methods in a python class instance. if value is not callable, returns value (python attribute from parent class). </li>
  <li><code>make_class (attributes, base_class = None)</code>: Sets the behaviour of a class, with ability to inherit from another class. Uses get and set to in a similar fashion to make_instance, except, it doesn't require a bind_method. It uses <code>init_instance(cls, *args)</code> to create a new object instance with arbitrary number of arguments (for methods of the attributes). <code>cls</code> argument for <code>init_instance</code> passes class dispatch dictionary to the object instance. Hence, the object 'inherits' (for lack of a better word) the class features. </li>
  <li><code>init_instance(cls, *args)</code>: Here I am a little unsure. Firstly, the function makes an instance with <code>instance = make_instance(cls)</code>, the instance inherits features of the class through <code>cls</code> dictionary. <code>init = cls['get']('__init__')</code> , <code>init</code> is created, a statement that looks up if <code>__init__</code> keyword was passed in attributes to <code>make_class</code>,  , <code>if init:    init(instance, *args)</code> makes args local to the instance? Returns an instance. </li>
  </ul>
  
  <h3>I can narrow down my question to -</h3>
  
  <p><code>init_instance</code> is a return to <code>new(*args)</code> in <code>make_class</code>. Which means an instance dictionary is returned to <code>new(*args)</code>. However, <code>make_class</code> returns <code>cls</code> which means we have to update <code>cls</code> somehow to contain <code>instance</code> properties. How is that that being done? It's most likely this statement <code>init (instance, *args)</code> but I don't know how to break down this statement. I haven't seen <code>init</code> as fn, how are arguments being passed to it? </p>