title: Create instance of class in another class (with generic example)
body: <p>I'm learning python via book and internet and I'm stuck on a class issue.</p>
  <p>2 questions:</p>
  <ol>
  <li>How do I create an instance of one class in another (separate) class?</li>
  <li>How do I pass variables between the class and the nested(?) class?</li>
  </ol>
  <p>When I try to create an instance of a class within another (separate) class, i'm able to do so within a method.  Here's the code:</p>
  <pre><code>import os
  
  class FOO():
      def __init__(self):
          self.values = [2, 4, 6, 8]
  
      def do_something(self, x, y):
          os.system(&quot;clear&quot;)
          z = self.values[x] * self.values[y]
          print &quot;%r * %r = %r&quot; % (self.values[x], self.values[y], z)
  
  
  class BAR():
      def __init__(self):
          pass
  
      def something_else(self, a, b):
          foo1 = FOO()
          foo1.do_something(a, b)
  
  bar = BAR()
  bar.something_else(1, 2)
  </code></pre>
  <p>Will this, however, allow me to access the FOO class and it's information in other BAR methods?  If so, how?</p>
  <p>I tried the following and got an error:</p>
  <pre><code>import os
  
  class FOO():
      def __init__(self):
          self.values = [2, 4, 6, 8]
  
      def do_something(self, x, y):
          os.system(&quot;clear&quot;)
          z = self.values[x] * self.values[y]
          print &quot;%r * %r = %r&quot; % (self.values[x], self.values[y], z)
  
  
  class BAR():
      def __init__(self):
          foo1 = FOO()
  
      def something_else(self, a, b):
          foo1.do_something(a, b)
  
  bar = BAR()
  bar.something_else(1, 2)
  </code></pre>
  <p>Here is the error:</p>
  <pre><code>Traceback (most recent call last):
  File &quot;cwic.py&quot;, line 22, in &lt;module&gt;
  bar.something_else(1, 2)
  File &quot;cwic.py&quot;, line 19, in something_else
  foo1.do_something(a, b)
  NameError: global name 'foo1' is not defined
  </code></pre>
  <p>I get the same error when I used:</p>
  <pre><code>def __init__(self):
      self.foo1 = FOO()
  </code></pre>
  <p>Also, how should I pass the 'values' from one class to the other?</p>
  <p>Please let me know if my general approach and/or syntax are wrong.</p>