Python... feeding an instance as an argument into a new instance.

Chris Roberts thecjguy1 at gmail.com
Sun Sep 3 07:35:52 EDT 2017


On Saturday, September 2, 2017 at 6:34:59 PM UTC-4, Chris Roberts wrote:
> Perhaps someone here could help me to get this into perspective. 
> Somehow when we start to feed an instance as the argument in a new instance. my head explodes..
> in this case...
> a = Foo()
> b = Bar(a)
> So... 
>  a is a 'Foo instance' with properties and methods. 
> b is a 'Bar instance'
> Since b is using the "a" instance as an argument?? 
> b=Bar(a)  has what?? 
> 
> Is there an easier way to put into perspective how an instance of one class is used as the argument into the instance of another class? 
> 
> Code below: 
> 
> class Foo(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.
>         # ^ double underscore (dunder) methods are usually special.  This one
>         #  gets called immediately after a new instance is created.
> 
>         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 #variable will be a property of "a" and self is the bound variable of a.
> 
>     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:
> #a is Foo, a has a method called method, and properties such as "variable and bar".
> # 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.
> print "##Above is all the a Foo object, and below is the b Bar object"
> 
> class Bar(object):
>     def __init__(self, arg):
>         self.arg = arg
>         self.Foo = Foo() #this calls and runs the Foo class again.
> 
> 
> b = Bar(a)  #b is a Bar object, a is a Foo object with properties and methods. then Bar() calls the Foo class again.
> b.arg.variable = "something"
> print a.variable  # something !(I don't know why "a" prints "something" here.)
> print b.Foo.variable  # Foo
> 
> ####
> OUTPUT: 
> Foo Bar
> Foo  Bar is now Baz
> Foo
> in method (args): 1 2
> in method (attributes): bar  Bar is now Baz
> in method (args): 1 2
> in method (attributes): bar  Bar is now Baz
> ##Above is all the a Foo object, and below is the b Bar object
> Foo Bar
> Foo  Bar is now Baz
> something
> Foo
> 
> #### Thanks in advance... crzzy1 ...

Steve and Irv, 
I see what you mean... I picked up on this example online and then tried to work it out on my own, but after seeing your replies, and looking further into cars examples I found how much more intuitive that makes it...
Anyways, now I know where to better redirect my studies and energies.
My work is sending me to a semi advanced (Core Python) class, and I am not ready, so trying to work on all my weak spots.
Thanks



More information about the Python-list mailing list