| | |
a class question
Thread Solved |
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 17
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.
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.
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.
[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!
•
•
•
•
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 :-)
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!
![]() |
Similar Threads
- Class question (PHP)
- Simple Class Question (C++)
- Passing Class Objects, Instances (C++)
- Passing Class Objects, Instances (C)
- Boundary Class and Broker Class (Computer Science)
Other Threads in the Python Forum
- Previous Thread: a need for help with MySQLdb
- Next Thread: Tinkering with Color (Python)
Views: 2089 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
app application array beginner c++ c/c++ change character class client code command compression convert count create csv ctypes database dictionary django dll error examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyqt python random raw_input recursion recursive redirect remote scrolledtext server socket ssh statistics stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode variable variables web windows wxpython






