back with more issues

Dave Angel davea at davea.name
Mon Aug 12 03:11:43 EDT 2013


Kris Mesenbrink wrote:

> darn i was hoping i could put off learning classes for a bit, but it seems that is not the case. i have tested it a bit and it seems to be working correctly now.
>
> ++++++++++++++++++++++++++++
> import random
>
> class player():
>     hp = 10
>     speed = 5
>     attack = random.randint(0,5)
>
> print (player.attack)
>
> +++++++++++++++++++++++++++++++++++
>
> i know it's not nearly as complicated as your examples but it seems to work. the self part of it always eluded me and continues to do so. and just so you know im learning through codecademy.com , it's based on python 2.7 and im trying to code in 3.3. but thanks for your help again and classes are starting (i think) to make some sort of sense.i'll have to reread both replies over and over again but it looks like a lot of useful info is there. but is the example i posted sorta right? i know i left the self part out but i think im on the right track.

The "complication" was there for good reason.

If you are sure you'll never have more than one player, this could work.
 i don't see the advantage over (ugh) global variables, however.

But what happens when you have four monsters instead of one?  A class
provides you a way to store data for each instance, not just for the
class as a whole.  And the self convention is kind of analogous to the
English "myself."  If you're inside an ordinary method, you refer to
yourself as "self."

By the way, by convention, class names are capitalized.  That's why i
called it Player.
-- 
DaveA





More information about the Python-list mailing list