title: Passing an instance of a class (object of a class) to another class
body: <p>What I do not understand is <code>b = Bar(a)</code>. What does it do? How is <code>Bar</code> taking <code>a</code> as an argument?</p>
  <p>Won't that mean <code>Bar</code> inherits from <code>a</code>? What is <code>Bar.Foo1 = Foo</code>? Does it mean <code>Foo1</code> is an instance of class <code>Foo()</code>? How do we access <code>Foo1</code> when it itself is an object? What is the meaning of <code>b.arg.variable</code>? Doesn't it mean that <code>b</code> has a method <code>arg</code> which has a variable called <code>variable</code>? The following code is from <a href="https://stackoverflow.com/a/11421962/3519372">this answer</a></p>
  <p>I just could not find parsing objects as an argument to another class.</p>
  <pre><code>  class Foo (object):
    #^class name  #^ inherits from object
  
        bar = &quot;Bar&quot; #Class attribute.
  
        def __init__(self):
            #        #^ The first variable is the class instance in methods.  
            #        #  This is called &quot;self&quot; by convention, but could be any name you want.
         
  
            self.variable=&quot;Foo&quot; #instance attribute.
            print self.variable, self.bar  #&lt;---self.bar references class attribute
            self.bar = &quot; Bar is now Baz&quot;   #&lt;---self.bar is now an instance attribute
            print self.variable, self.bar  
  
         def method(self,arg1,arg2):
            #This method has arguments. You would call it like this : instance.method(1,2)
            print &quot;in method (args):&quot;,arg1,arg2
            print &quot;in method (attributes):&quot;, self.variable, self.bar
  
  
   a=Foo() # this calls __init__ (indirectly), output:
               # Foo bar
               # Foo  Bar is now Baz
   print a.variable # Foo
   a.variable=&quot;bar&quot;
   a.method(1,2) # output:
                 # in method (args): 1 2
                 # in method (attributes): bar Bar is now Baz
   Foo.method(a,1,2) #&lt;--- Same as a.method(1,2).  This makes it a little more explicit what the argument &quot;self&quot; actually is.
  
   class Bar(object):
       def __init__(self,arg):
            self.arg=arg
            self.Foo1=Foo()
  
   b=Bar(a)
   b.arg.variable=&quot;something&quot;
   print a.variable # something
   print b.Foo1.variable # Foo
  </code></pre>