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 ... -- https://mail.python.org/mailman/listinfo/python-list