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
Passing object as parameter In class Person, MyClass is also used so that is imported. In method display() object of MyClass is created. Then the my_method() method of class MyClass is called and object of Person class is passed as parameter. On executing this Python program you get output as following.
To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.
"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?".
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With