title: Python - why can I call a class method with an instance?
body: <p>New to Python and having done some reading, I'm making some methods in my custom class class methods rather than instance methods.</p>
  
  <p>So I tested my code but I hadn't changed some of the method calls to call the method in the class rather than the instance, but they still worked:</p>
  
  <pre><code>class myClass:
     @classmethod:
     def foo(cls):
        print 'Class method foo called with %s.'%(cls)
  
     def bar(self):
        print 'Instance method bar called with %s.'%(self)
  
  myClass.foo()
  thing = myClass()
  thing.foo()
  thing.bar()
  </code></pre>
  
  <p>This produces:</p>
  
  <pre><code>class method foo called with __main__.myClass.
  class method foo called with __main__.myClass.
  instance method bar called with &lt;__main__.myClass instance at 0x389ba4&gt;.
  </code></pre>
  
  <p>So what I'm wondering is why I can call a class method (foo) on an instance (thing.foo), (although it's the class that gets passed to the method)? It kind of makes sense, as 'thing' is a 'myClass', but I was expecting Python to give an error saying something along the lines of 'foo is a class method and can't be called on an instance'.</p>
  
  <p>Is this just an expected consequence of inheritance with the 'thing' object inheriting the foo method from its superclass?</p>
  
  <p>If I try to call the instance method via the class:</p>
  
  <pre><code>myClass.bar()
  </code></pre>
  
  <p>then I get:</p>
  
  <pre><code>TypeError: unbound method bar() must be called with myClass instance...
  </code></pre>
  
  <p>which makes perfect sense.</p>