title: functions and class attributes (python)
body: <pre><code>def add_age(cls,age):
      cls.yrs_old = age
      return cls
  
  
  class Test:
      age = add_age
  
  a = Test()
  a.age(5)
  
  
  print(a.yrs_old)
  </code></pre>
  
  <p>So I was toying around with python and this is something I've never encountered before</p>
  
  <p>here I defined a global function called <code>add_age</code>
  assigned <code>add_age</code> to the <code>age</code> attribute of <code>Test</code></p>
  
  <p>what surprises me is that <code>add_age</code> takes 2 arguments <code>cls</code> and <code>age</code> so I tried this</p>
  
  <pre><code>a = Test
  a.age(a,5)
  print(a.yrs_old)
  </code></pre>
  
  <p>works as expected however doing this:</p>
  
  <pre><code>a = Test()
  a.age(a,5)
  print(a.yrs_old)
  </code></pre>
  
  <p>throws an error <code>TypeError: add_age() takes 2 positional arguments but 3 were given</code></p>
  
  <p>so then I said well let's try taking out one argument
  so I did this:</p>
  
  <pre><code>a = Test()
  a.age(5)
  print(a.yrs_old)
  </code></pre>
  
  <p>The class itself is automatically passed as the first argument.almost like <code>self</code> and the <code>age</code> attribute now acts as regular <code>class method</code></p>
  
  <p>which to me as a python newbie is mind boggling</p>
  
  <p>can someone give some insight on what is happening?</p>