[Tutor] Re: Help with classes (Joseph Q.)
Joseph Quigley
cpu.crazy at gmail.com
Sat Apr 9 00:46:33 CEST 2005
class Message:
def init(self, p = 'Hello world'):
self.text = p
def sayIt(self):
print self.text
m = Message()
m.init()
m.sayIt()
m.init('Hiya fred!')
m.sayIt()
> > Well this OOP stuff is realy hard for me as I have never even
> > programmed it took me a while just to understand defs.
>
>That's OK, OOP is quite a strange concept for many folks. Its
>actually easier to learn as a beginner than for folks who
>have been programming without OOP for a long time!
>
> > determined to learn how to do it. My biggest problem is with
>__init__
> > I still don't understand how to use it.
>
>init is simply where you initialise the data attributes of your class.
>
>It could be defined as a normal method and you call it explicitly:
>
>class Message:
> def init(self, txt = 'Hello world'):
> self.text = txt
> def sayIt(self):
> print self.text
>
>And every time you create a message object you explicitly call it:
>
>m = Message()
>m.init('Hiya fred!')
>m.sayIt()
I was having problems with the __init__ too.
class Message:
def init(self, txt = 'Hello world'):
self.text = txt
def sayIt(self):
print self.text
And every time you create a message object you explicitly call it:
m = Message()
m.init()
m.sayIt()
m.init("Hello World is so overdone. It hard to break tradition though :->")
m.sayIt()
Why did you put 'Hiya fred!' in place of having m.init print "Hello World"?
Now what are dictionaries and the __name__ really used for? A byte of
python never got that through my thick skull.
More information about the Tutor
mailing list