Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39073

Re: Struggling with program

References <5d90a61d-f122-4dd4-8d79-c9909662b2bc@googlegroups.com>
Date 2013-02-18 17:49 +1100
Subject Re: Struggling with program
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1927.1361170192.2939.python-list@python.org> (permalink)

Show all headers | 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 | NextPrevious in thread | Find similar | Unroll thread


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