Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39073
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'string.': 0.04; 'c++,': 0.07; 'constructor': 0.07; 'python': 0.09; '__init__': 0.09; 'arguments,': 0.09; 'defined.': 0.09; 'none.': 0.09; 'zero.': 0.09; "hasn't": 0.15; 'value.': 0.15; '"normal"': 0.16; '1024': 0.16; '3):': 0.16; 'assignment?': 0.16; 'assigns': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'initializes': 0.16; 'parameter,': 0.16; 'subclassing': 0.16; 'subject:program': 0.16; 'mon,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'instance': 0.17; 'feb': 0.19; 'variable': 0.20; 'otherwise,': 0.20; 'parameters': 0.20; 'sort': 0.21; 'java': 0.21; 'explicit': 0.22; 'object.': 0.22; 'player': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'wrote': 0.26; 'checking': 0.27; 'possibly': 0.27; 'convention': 0.27; 'message- id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'initialized': 0.29; 'optional': 0.29; 'definition': 0.29; 'probably': 0.29; 'class': 0.29; 'saves': 0.30; 'code': 0.31; 'int': 0.33; 'to:addr:python-list': 0.33; '(with': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'thanks': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'method': 0.36; 'subject:with': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'you.': 0.61; 'situation': 0.62; 'details': 0.63; 'more': 0.63; 'benefit': 0.70; 'sounds': 0.71; 'score': 0.75; '2013': 0.84; 'idiomatic': 0.84; 'score:': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=pxOAXlQ1kxVb0wzAo0Uw8Ec32z1oey5Svw8S9ab4VcY=; b=z3E7s0qsTvzQoMmULOmf4QwhiOoOH+yA6c6P9fTAj1vIacQvhvfNh6wweAdDJzJD+0 VW9Px95fQnyeXRLMSqwfbmNSz4oUQAsOkrIm++9yfUs/LyM7icx9P7QNkDrtDCCQoCG6 NHTPlcHX0hpQ2GHhuEn/85C0ZLhFywju2F5yZVVh0H1V5HZXLVlFEPkuneVtAYX4ttCv 6tJmM2Nhdm5bMnRvP0sdk1IbCOwXFYkO0zPRJa/7cUHKD9z0J4XvjWmlBoSuccusoQFq hQpyizX2Zm3XQymxEGjMwbTW2mqbAXD9xmqSIdEQs0PgST5v/Kcu0M1kV4gVQifo6tNa Gh4g== |
| MIME-Version | 1.0 |
| X-Received | by 10.52.22.194 with SMTP id g2mr12537935vdf.91.1361170184123; Sun, 17 Feb 2013 22:49:44 -0800 (PST) |
| In-Reply-To | <5d90a61d-f122-4dd4-8d79-c9909662b2bc@googlegroups.com> |
| References | <5d90a61d-f122-4dd4-8d79-c9909662b2bc@googlegroups.com> |
| Date | Mon, 18 Feb 2013 17:49:44 +1100 |
| Subject | Re: Struggling with program |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1927.1361170192.2939.python-list@python.org> (permalink) |
| Lines | 50 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1361170192 news.xs4all.nl 6887 [2001:888:2000:d::a6]:60778 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:39073 |
Show key headers only | View raw
On Mon, Feb 18, 2013 at 1:29 PM, maiden129 <sengokubasarafever@gmail.com> wrote:
> Write the definition of a class Player containing:
> An instance variable name of type String , initialized to the empty String.
> An instance variable score of type int , initialized to zero.
> A method called set_name that has one parameter, whose value it assigns to the instance variable name .
> A method called set_score that has one parameter, whose value it assigns to the instance variable score .
> A method called get_name that has no parameters and that returns the value of the instance variable name .
> A method called get_score that has no parameters and that returns the value of the instance variable score .
> No constructor need be defined.
Is this actually a Python assignment? This sounds decidedly
un-Pythonic. A much more idiomatic way to write this would simply be
(with the explicit subclassing of object optional in Python 3):
class Player(object):
pass
You can then create a player thus:
fred = Player()
And set his name and score:
fred.name = "Fred"
fred.score = 1024
And get them just as easily:
print("Name: "+fred.name)
print("Score: "+str(fred.score))
Note how much code I wrote to make all this possible: None. In Java
and C++, the convention is to hide internal members, on the off-chance
that you might need to change a trivial getter/setter pair into
something more complicated (though in my experience, this almost never
can be done so simply even if you _have_ the getter and setter - the
only situation that that model serves is logging/breakpoints); in
Python, you get the same benefit without them, thanks to some spiffy
features that you probably don't need to worry about the details of at
the moment.
The only thing that I might want to change in the above code is to
create an __init__ member that initializes all the "normal" members -
possibly to its arguments, letting you do fred=Player("Fred",1024), or
possibly to some sort of default value. This serves as documentation
and also saves checking for AttributeError when reading from the
object. But otherwise, there's nothing in there that Python hasn't
already done for you.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Struggling with program maiden129 <sengokubasarafever@gmail.com> - 2013-02-17 18:29 -0800 Re: Struggling with program Dave Angel <davea@davea.name> - 2013-02-17 21:49 -0500 Re: Struggling with program Michael Torrie <torriem@gmail.com> - 2013-02-17 20:49 -0700 Re: Struggling with program Chris Angelico <rosuav@gmail.com> - 2013-02-18 17:49 +1100
csiph-web