Skip to content

Instantly share code, notes, and snippets.

@stormbrew
Created September 24, 2012 22:34
Show Gist options
  • Save stormbrew/3778876 to your computer and use it in GitHub Desktop.
Save stormbrew/3778876 to your computer and use it in GitHub Desktop.
Python object model in a nutshell (with deliberate errors for simplicity):
- A class is a dict
- The dict contains methods (not in python syntax):
cls = { 'a': function(self, blah) {...}, 'b': function(self, blorp) {...} }
- An object is also a dict that contains an __class__ member that points to the class:
obj = { '__class__': cls }
- When you fetch a method from obj:
obj.a
it looks on __class__ for 'a', and finding it wraps it in a functor that adds the self parameter:
function(blah) { obj['__class__']['a'](obj, blah) }
- When you apply to that function by calling it:
obj.a(1)
you are actually calling the above functor that prefixes the self parameter in.
This is why in python a class method is defined like this:
def blah(self, blah):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment