a class question

Thread Solved

Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 17
shanenin shanenin is offline Offline
Posting Whiz in Training

a class question

 
0
  #1
Jul 1st, 2005
I am slowly absorbing these python class concepts(kinda)

this fails with an error
[php]
class Critter(object):

def __init__(self):
print "I am born"


def talk(self,name):
self.test = name
print self.test

crit = Critter('ralph')
crit.talk()[/php]

this one works
[php]
class Critter(object):

def __init__(self,name):
print "I am born"
self.test = name
print self.test

def talk(self):
print 'ya'


crit = Critter('ralph')
crit.talk()[/php]

Is the following statment true for the most part.

if a parameter is passed directly to the object, it is passed down to the __init__ constuctor only, not to any other methods. That would mean the only way to get data into an object is thru the __init__ constructor.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,669
Reputation: vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light 
Solved Threads: 1095
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: a class question

 
0
  #2
Jul 2nd, 2005
Depends on the way you are calling the class, look at it this way (check my comments) ...
[php]class Critter:

def __init__(self,name):
print "I am born"
self.test = name
print self.test

def talk(self):
print 'ya'


crit = Critter('ralph') # like calling Critter.__init__('ralph')
crit.talk() # like calling Critter.__init__('ralph').talk()
[/php]
If you want to do it the other way, you have to pass the name in talk() ...
[php]class Critter:

def __init__(self):
print "I am born"


def talk(self,name):
self.test = name
print self.test
print 'ya'

crit = Critter()
crit.talk('ralph') # like calling Critter.__init__().talk('ralph')
[/php]
Which calling method you want to apply to the class depends on the circumstances. Look at the code snippet:
http://www.daniweb.com/code/snippet287.html
where the first calling method makes more sense.

Edit note: don't actually use Critter.__init__().talk('ralph'), the interpreter will balk at that construction, it just illustrates the sequence of calls.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 17
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: a class question

 
0
  #3
Jul 2nd, 2005
look ate this line of code you made
[php]crit.talk('ralph') # like calling Critter.__init__().talk('ralph')[/php]

why is the __init__() method invoved. It seems to be using the Critter class and the talk method only.


edit added later//

I am looking at the link to the code snippet now :-)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,669
Reputation: vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light 
Solved Threads: 1095
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: a class question

 
0
  #4
Jul 2nd, 2005
Originally Posted by shanenin
look ate this line of code you made
[php]crit.talk('ralph') # like calling Critter.__init__().talk('ralph')[/php]

why is the __init__() method invoved. It seems to be using the Critter class and the talk method only.


edit added later//

I am looking at the link to the code snippet now :-)
The object crit is an alias of Critter(), which in turn is equivalent to Critter.__init__(). I shouldn't have used Critter.__init__().talk('ralph') the interpreter will balk at that construction. This just illustrates the sequence it calls.

To clear things up, whenever you invoke the class it will look at __init__() first, but only if it's there. You could have written the class Critter this way ...
[php]class Critter:

def talk(self,name):
print "I am born"
self.test = name
print self.test
print 'ya'

crit = Critter()
crit.talk('ralph')
[/php]
The method/function __init_() is only needed, if there are several other methods in the class that need common information.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 17
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: a class question

 
0
  #5
Jul 2nd, 2005
I think i am following :-)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,669
Reputation: vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light 
Solved Threads: 1095
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: a class question

 
0
  #6
Jul 2nd, 2005
Thanks for asking, it made me think and cleared things up in my mind too!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2089 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC