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

Rick Johnson rantingrickjohnson at gmail.com
Sun Sep 3 08:22:42 EDT 2017


On Saturday, September 2, 2017 at 7:45:14 PM UTC-5, Steve D'Aprano wrote:
> On Sun, 3 Sep 2017 08:34 am, 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??
> 
> It has attributes and methods, same as any other instance
> of any other class.  I think you are overthinking it. Or
> perhaps underthinking it. In either case, using generic
> nonsense classes like Foo and Bar adds no understanding.

Yes. Even for seasoned programmers, generic names can
obfuscate the overall model. And such names have no business
in beginner, or even intermediate, coarses.

> Suppose you build an Engine class:
> 
> class Engine:
>     def __init__(self, fuel, capacity):
>         self.sparkplugs = [SparkPlug() for i in range(4)]

Steven, why am i _not_ surprised that you drive a four-banger? ;-)

>         ...
>     def start(self):
>         ...
>     def stop(self):
>         ...
> 
> So Engines have methods, and attributes such as capacity,
> the kind of fuel they run on (petrol/gasoline, diesel or
> LPG), etc. Some attributes (such as the sparkplugs) are
> themselves instances of another class.  Now we build a car
> out of other objects:
> 
> class Car:
>     def __init__(self, colour, engine, chassis, seats, dashboard, wheels, 
>                  brakes, parkingbrake, airbags, entertainment_system):
>         self.colour = colour
>         self.body = chassis.add_panels()
>         self.engine = engine
>         ...
>     def drive(self):

This should be named "forward", as: (1) such a name meshes
semantically against the "reverse" method, and (2) such a
name clearly defines an intuitive vector path that is
consistent with the car's three dimensional orientation,
_orientation_ which is constrained by the mechanical
capabilities of the car, and _mechanical_capabilities_, which
are further constrained by the laws of physics. "Drive" is
just too ambiguous.

>         ...
>     def reverse(self):
>         ...
>     def engage_parking_brake(self):
>         ...
> 
> So Cars have methods, and attributes such as colour,
> engine, brakes, airbags, etc. Some of those attributes are
> themselves made of instances of another class.  In fact
> even "primitive" values such as ints, floats, lists,
> strings, tuples, boolean flags etc are objects in Python.
> In Python, it is objects all the way down: all values are
> objects.

Overall though, a very good introductory example of OOP.
Good work Steven! We're going to make an OOP fan out of you
yet, and given enough time, heck, you may even begin to
appreciate the purity of Java. ;-)




More information about the Python-list mailing list