I am having a tough time understanding what is going on in this following code

class Player(object):

    """ A player in a shooter game. """

    def blast(self, enemy):

        print "The player blasts an enemy.\n"

        enemy.die()


class Alien(object):

    """ An alien in a shooter game. """

    def die(self):

        print "The alien gasps and says, 'Oh, this is it.  This is the big one. \n" \

              "Yes, it's getting dark now.  Tell my 1.6 million larvae that I loved them... \n" \

              "Good-bye, cruel universe.'"

# main

print "\t\tDeath of an Alien\n"


hero = Player()

invader = Alien()

hero.blast(invader)

in particular this line

hero.blast(invader)

how is the object invader interacting the the blast method?

Edited 7 Years Ago by vegaseat: fixed code tags

The last three lines of your code are equal to writing ...

Player().blast(Alien())

Maybe this shows the connection a little better. Player().blast has the line enemy.die() in it. We tell blast that the enemy is the Alien(), so it calls Alien().die().

BTW, the print statement with the three lines of text will give you an error. Change it to a triple quote, or add two more print.

That is odd, I thought I just copied and pasted the scripts how is was from the book I was following. The original code was like this, I am not sure how I lost the characters

it must be a problem with the message board these characters ' \' at the end of the print statment are being cut off.

Testing ...

class Alien(object): 
    """ An alien in a shooter game. """ 
    def die(self): 
        # adding three lines to one print statement
        print "The alien gasps and says, 'Oh, this is it.  This is the big one. \n"\
              "Yes, it's getting dark now.  Tell my 1.6 million larvae that I loved them... \n"\
              "Good-bye, cruel universe.'"

try doing your test with php code tags, they seem to make a difference.

class Alien(object): 
    """ An alien in a shooter game. """ 
    def die(self): 
        # adding three lines to one print statement
        print "The alien gasps and says, 'Oh, this is it.  This is the big one. \n"\
              "Yes, it's getting dark now.  Tell my 1.6 million larvae that I loved them... \n"\
              "Good-bye, cruel universe.'"

I just figured that out too!
Thanks ...

I just put a hint into the Starting Python sticky about multiline strings. It looks like the php code field doesn't like trailing \ characters on a line! Bummer, I like the color!!!

Edit: I informed the gurus at DaniWeb about this. Let's see if anything can be done about it.

This is actually the exact same chapter I am working on in Python for the Absolute Beginner (Chapter 9). The hero invokes its blast method, with invader as its argument. The hero's blast method invokes the invader's die method, which prints the death message.

Hope that helps. I'm new at this as well.

I will be honest, it is getting a little tough for me to follow, how is it coming along for you?

This is pretty confusing isn't it? I am still not quite sure how the author is creating card objects without assigning them to a variable (on page 272) in the Deck class. I get how the rest of the Blackjack program works, but if somebody could explain that one particular to me...

Nevermind, I found a bit of inherited code that explains it. I guess Python can use the list position to call an object!

This question has already been answered. Start a new discussion instead.