title: Python: how to call class methods through a class object passed as a parameter
body: <p>I would like to pass a class as an argument to another class and then call methods of the class that I passed but I am getting different errors. I started with <a href="https://stackoverflow.com/questions/1436444/python-passing-a-class-name-as-a-parameter-to-a-function">this</a> but my code keeps giving me errors.</p>
  
  <p>Consider the following situation:</p>
  
  <pre><code>class A:
      A_class_var = "I am an A class variable"
  
      def __init__(self):
          self.A_inst_var = "I am an A instance variable"
  
      def GetClassVar(self):
          return A_class_var
  
      def GetInstVar(self):
          return self.A_inst_var
  
  class B:
  
      def __init__(self, cls):
          print cls.GetClassVar()
          print cls.GetInstVar()
  </code></pre>
  
  <p>1) When I call <code>b = B(A)</code> I get an "unbound method GetClassVar() must be called with A instance as first argument (got nothing instead)." So I figure that GetClassVar() and GetInstVar() expected an A instance and tried adding <code>cls</code> to the arguments of the two calls in B's <code>__init__</code>. Same error.</p>
  
  <p>2) When I tried calling <code>b = B(A())</code> I get the error "global name A_class_var" is not defined.</p>
  
  <p>3) When I tried calling <code>a = A()</code> followed by <code>b = B(a)</code> I got the same error as in #2. </p>
  
  <p>4) When I tried to do the suggestion in the SO answer I linked above, namely change the definition of <code>B</code> to:</p>
  
  <pre><code>class B:
      def __init__(self, cls):
          self.my_friend = cls
  
      def GetFriendsVariables(self):
          print my_friend.GetClassVar()
          print my_friend.GetInstVar()
  </code></pre>
  
  <p>and then call <code>b = B(A)</code> followed by <code>b.GetFriendsVariables()</code> (also following that SO I linked) I get the error "global name my_friend is not defined" in <code>GetFriendsVariables</code>.</p>
  
  <p>I'd really like to understand how to properly pass classes as arguments so that I can <em>access</em> variables and methods of those classes and would love to understand why my various attempts above don't work (and yet the SO link I sent does?)</p>
  
  <p>Thanks very much!</p>