14

What I do not understand is b = Bar(a). What does it do? How is Bar taking a as an argument?

Won't that mean Bar inherits from a? What is Bar.Foo1 = Foo? Does it mean Foo1 is an instance of class Foo()? How do we access Foo1 when it itself is an object? What is the meaning of b.arg.variable? Doesn't it mean that b has a method arg which has a variable called variable? The following code is from this answer

I just could not find parsing objects as an argument to another class.

  class Foo (object):
  #^class name  #^ inherits from object

      bar = "Bar" #Class attribute.

      def __init__(self):
          #        #^ The first variable is the class instance in methods.  
          #        #  This is called "self" by convention, but could be any name you want.
       

          self.variable="Foo" #instance attribute.
          print self.variable, self.bar  #<---self.bar references class attribute
          self.bar = " Bar is now Baz"   #<---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 "in method (args):",arg1,arg2
          print "in method (attributes):", self.variable, self.bar


 a=Foo() # this calls __init__ (indirectly), output:
             # Foo bar
             # Foo  Bar is now Baz
 print a.variable # Foo
 a.variable="bar"
 a.method(1,2) # output:
               # in method (args): 1 2
               # in method (attributes): bar Bar is now Baz
 Foo.method(a,1,2) #<--- Same as a.method(1,2).  This makes it a little more explicit what the argument "self" actually is.

 class Bar(object):
     def __init__(self,arg):
          self.arg=arg
          self.Foo1=Foo()

 b=Bar(a)
 b.arg.variable="something"
 print a.variable # something
 print b.Foo1.variable # Foo
qurius
  • 187
  • 1
  • 1
  • 5
  • 1
    You should read [the Python tutorial](http://docs.python.org/2/tutorial/) to familiarize yourself with the basics of Python. – BrenBarn Jun 14 '14 at 19:54
  • 2
    I read that . There is no such thing as passing an object to another class. And I was unable to get from other sources – qurius Jun 14 '14 at 19:55
  • 3
    It is described [here](https://docs.python.org/2/tutorial/classes.html#class-objects). Everything is an object in Python. Passing an object to a class is no different from passing an integer (as in that example), a string, or anything else. – BrenBarn Jun 14 '14 at 19:57
  • Then b is an object of class Bar whose constructor will be called to initialize b, a`s(Foo()) or Bar()? and what is b.arg.variable? – qurius Jun 14 '14 at 20:04
  • 1
    I don't understand what you're asking. `a` was already created with the line `a = Foo()`, and Foo's `__init__` was called at that time. When `Bar(a)` is executed, then Bar's `__init__` will be called. The comments in your posted example seem to already mostly answer the questions you're asking. – BrenBarn Jun 14 '14 at 20:09
  • Bar`s `__init__` would have been called even when it was `Bar(a)`.? What role does passing object `a` as an argument in `Bar()` play? Could you please explain `b.arg.variable`? is `arg` = `a` , like any other argument? Doesnt it make arg as an object ? – qurius Jun 14 '14 at 20:15
  • I don't understand what "Doesn't it make arg as an object" means. – BrenBarn Jun 14 '14 at 20:24

2 Answers2

14

"What I do not understand is b = Bar(a). What does it do?"

b = Bar(a) does two things. First, it creates an object of class Bar (with any class variables and methods attached). Then, it runs __init__ with the first argument (self) pointing to the object that was just created, and with a as the second argument (arg). While running __init__, as one of the commands in that method, it sets self.arg to point to the object pointed to by arg (i.e. to the object pointed to by the variable a). Finally, the variable b is set to refer to the object that was created.

It may help to think this way: a variable in Python is really just a pointer that points to an object. You can have more than one variable pointing to the same object. In this case, a and b.arg both point to the same object.

I found this sort of thing confusing too, at first. I had seen the advice to think of variables as separate concepts from the object they point to and ignored it as unnecessarily complicating things, but I had to go back to accepting that way of thinking in order to make sense of things. People do often use the variable as a name to refer to the object it points to; you just have to know when to take this literally or not.

"Wont that mean Bar inherit from a?"

No. If a is a class, then class Bar(a) would mean that Bar inherits from a. But in b = Bar(a), a is an object being passed as an argument to __init__.

"What is Bar.Foo1 = Foo?"

Sorry -- I don't see that in the example code you gave.

"What is the meaning of b.arg.variable?"

b is an object (i mean, b refers to an object) and b.arg is one of the attributes of that object. Methods and variables are different types of attributes. In this case, b.arg is a variable pointing to an object. The object referred to by b.arg has attribute variable, which is a variable.

b.arg refers to the same object that a refers to, therefore b.arg.variable is the same variable as a.variable. It not only points to the same object, but actually is the same variable. The object it points to is the string "something".

@Brenbarn: I think that's what quirius meant by "Wont that mean Bar inherit from a?".

Linguist
  • 849
  • 7
  • 5
12

Here is a simpler example. Suppose you have these classes:

class Foo(object):
    pass

class Bar(object):
    def __init__(self, arg):
        self.arg = arg

Here are two things you could do:

# Option 1: pass in an integer
>>> b = Bar(1)
>>> b.arg
1

# Option 2: pass in a Foo object
>>> a = Foo()
>>> b = Bar(a)
>>> b.arg
<__main__.Foo object at 0x0000000002A440F0>

There is no difference in how the two cases are handled. Passing in a (a Foo object) is no different from passing in an integer. All that Bar does is store the value that is passed in, and it can store it just the same whether it is a Foo or an int. When you call Bar(something), it is entirely up to the Bar class how to handle the object that is passed in. The type of the passed-in object is not involved except insofar as Bar chooses to explicitly involve it (e.g., by calling methods on the passed in object).

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Thank you. and I apologize for asking trivial questions but I just could not get out of the java frame to think variable as a variable and not as an object. – qurius Jun 14 '14 at 20:31
  • @qurius: If I understand right, it now appears you were really asking about whether `Bar(a)` *creates* a new object that is somehow "based on" `a` (e.g., a copy of `a`). Might have been good to mention that! :-) When you just ask "what does Bar(a) do", it's not clear what about it you don't understand. – BrenBarn Jun 14 '14 at 20:34
  • Yes , this indeed was what I meant to ask. :) – qurius Jun 14 '14 at 20:39
  • 1
    So is the `(object)` part in the class definition ever actually needed? – Austin Dec 20 '17 at 01:38