943,866 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2183
  • Python RSS
Jul 2nd, 2005
0

a class question

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jul 2nd, 2005
0

Re: a class question

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 2nd, 2005
0

Re: a class question

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 :-)
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jul 2nd, 2005
0

Re: a class question

Quote 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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 2nd, 2005
0

Re: a class question

I think i am following :-)
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jul 2nd, 2005
0

Re: a class question

Thanks for asking, it made me think and cleared things up in my mind too!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: a need for help with MySQLdb
Next Thread in Python Forum Timeline: Tinkering with Color (Python)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC