Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!ra.nrl.navy.mil!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Classes Date: Fri, 31 Oct 2014 17:43:35 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 54 Message-ID: References: <51755at03r0bidjqh3qf0hhpvjr8756ill@4ax.com> <545350c3$0$23449$426a74cc@news.free.fr> <5453c1a6$0$12981$c3e8da3$5496439d@news.astraweb.com> <7pg75alj1qn97hlirk81f7nqd265emgu05@4ax.com> NNTP-Posting-Host: 67-130-15-94.dia.static.qwest.net X-Trace: reader1.panix.com 1414777415 11633 67.130.15.94 (31 Oct 2014 17:43:35 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Fri, 31 Oct 2014 17:43:35 +0000 (UTC) User-Agent: slrn/1.0.1 (Linux) Xref: csiph.com comp.lang.python:80042 On 2014-10-31, Seymore4Head wrote: > On Sat, 01 Nov 2014 04:06:44 +1100, Steven D'Aprano >>Let's make the class a bit easier to use, at the expense of doing a bit more >>work up front: >> >>class MyClass: >> def __init__(self, value, message): >> self.value = value >> self.message = message >> >>obj = MyClass(23.0, "hello world") >>print(obj.value) >>print(obj.message) >> >> >>The __init__ method is automatically called when you call the class as if it >>were a function. Because the __init__ method has two arguments (plus the >>special "self" argument), you have to call the class with two arguments. >>They get used as the value and message respectively. >> >>Or we can give it getters and setters: >> >>class MyClass: >> def set_value(self, value): >> self.value = value >> def get_value(self): >> return self.value >> def set_message(self, message): >> self.message = message >> def get_message(self): >> return self.message >> >>obj = MyClass() >>obj.set_value(23.0) >>obj.set_message("hello world") >>print(obj.get_value()) >>print(obj.get_message()) >> >> >>If you're thinking that's a lot of extra work for not much benefit, 99.99% >>of the time you're right. > > I agree it is more work. But more work means more practice. I need > more practice figuring out how these commands work. Except you're practicing doing things the "wrong" way. If you want to learn Java, then use Java. If you want to learn Python, then don't write Java. -- Grant Edwards grant.b.edwards Yow! HOORAY, Ronald!! at Now YOU can marry LINDA gmail.com RONSTADT too!!