[Tutor] Trouble with exercise regarding classes

Alan Gauld alan.gauld at btinternet.com
Thu Aug 26 10:32:55 CEST 2010


"Andrew Martin" <amartin7211 at gmail.com> wrote

> I want to compare zenith, a floating point number, with the current 
> y value?
> I thought the current y value could be retrieved by Projectile.getY.

Projectile.getY is a reference to the getY method of the Projectile 
class.
(See the separate thread on function objects for more on this topic)

You want to execute the method so you need parens on the end.

But, you also want to execute it for the cball instance.
You have already done this earlier in your code, here:

>>>>    while cball.getY() >= 0:

So you just need to make the if test compatible with that:


>>         if Projectile.getY > zenith:

becomes

            if cball.getY() > zenith:

And similarly for the assignment

>>>>            zenith = Projectile.getY()
becomes
                    zenith = cball.getY()


As an aside you can do this using Projectile, but its bad practice:

Projectile.getY(cball)

This explicitly provides the self argument instead of Python doing
it for you when you use the instance. We can use this technique when
calling inherited methods inside a class method definition. Anywhere
else its best to use the instance to call a method.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list