RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 1617 | Replies: 9
Reply
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

help me understand what is happening

  #1  
Jul 21st, 2005
I am having a tough time understanding what is going on in this following code
[php]
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)
[/php]

in particular this line
[php]hero.blast(invader)[/php]
how is the object invader interacting the the blast method?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Posts: 2,548
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: help me understand what is happening

  #2  
Jul 21st, 2005
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.
May 'the Google' be with you!
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: help me understand what is happening

  #3  
Jul 21st, 2005
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.
Reply With Quote  
Join Date: Oct 2004
Posts: 2,548
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: help me understand what is happening

  #4  
Jul 22nd, 2005
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.'" 
May 'the Google' be with you!
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: help me understand what is happening

  #5  
Jul 22nd, 2005
try doing your test with php code tags, they seem to make a difference.
Reply With Quote  
Join Date: Oct 2004
Posts: 2,548
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: help me understand what is happening

  #6  
Jul 22nd, 2005
[php]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.'"
[/php]
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.
May 'the Google' be with you!
Reply With Quote  
Join Date: Jul 2005
Posts: 5
Reputation: Walrus Bites is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Walrus Bites's Avatar
Walrus Bites Walrus Bites is offline Offline
Newbie Poster

Re: help me understand what is happening

  #7  
Jul 27th, 2005
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.
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: help me understand what is happening

  #8  
Jul 29th, 2005
I will be honest, it is getting a little tough for me to follow, how is it coming along for you?
Reply With Quote  
Join Date: Jul 2005
Posts: 5
Reputation: Walrus Bites is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Walrus Bites's Avatar
Walrus Bites Walrus Bites is offline Offline
Newbie Poster

Re: help me understand what is happening

  #9  
Jul 31st, 2005
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...
Reply With Quote  
Join Date: Jul 2005
Posts: 5
Reputation: Walrus Bites is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Walrus Bites's Avatar
Walrus Bites Walrus Bites is offline Offline
Newbie Poster

Re: help me understand what is happening

  #10  
Aug 2nd, 2005
Nevermind, I found a bit of inherited code that explains it. I guess Python can use the list position to call an object!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:12 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC