I am slowly absorbing these python class concepts(kinda)

this fails with an error

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()

this one works

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()

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.

Depends on the way you are calling the class, look at it this way (check my comments) ...

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()

If you want to do it the other way, you have to pass the name in talk() ...

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')

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.

look ate this line of code you made

crit.talk('ralph')   #  like calling Critter.__init__().talk('ralph')

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 :-)

look ate this line of code you made

crit.talk('ralph')   #  like calling Critter.__init__().talk('ralph')

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 ...

class Critter: 

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

crit = Critter()
crit.talk('ralph')

The method/function __init_() is only needed, if there are several other methods in the class that need common information.

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