So far, we've only looked at binding various kinds of ``helper" attributes into components, but not assembling components themselves into larger components or applications.
One issue that arises quite a bit in the assembly of components is the notion of context. A component often needs to know ``where" it is located, in the sense of its placement in a larger component. While components will typically have many connections to other components, the connection between a component and its container or parent component is particularly important. Let's look at an example:
>>> class Wheel(binding.Component): def spin(self): print "I'm moving at %d mph" % self.getParentComponent().speed >>> class Car(binding.Component): wheel = binding.Make(Wheel) speed = 50 >>> c = Car() >>> c.wheel.spin() I'm moving at 50 mph
The binding.Component class defines two methods for dealing with parent components: getParentComponent and setParentComponent. setParentComponent is automatically called for you when you create an instance via binding.Make, which is why our Wheel instance above was able to access its parent component with getParentComponent. Let's look at a slightly different version of that example:
>>> class Wheel(binding.Component): def spin(self): print "I'm moving at %d mph" % self.speed speed = binding.Obtain('../speed') >>> class Car(binding.Component): wheel = binding.Make(Wheel) speed = 50 >>> c = Car() >>> c.wheel.spin() I'm moving at 50 mph
By now, perhaps the wheels in your brain will be spinning as well, thinking about the possibilities here.