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


Groups > comp.lang.python > #39064

Re: Struggling with program

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <torriem@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; 'string.': 0.04; 'initialize': 0.05; '"__main__":': 0.07; '"c"': 0.07; '__name__': 0.07; 'assign': 0.07; 'attributes': 0.07; 'constructor': 0.07; 'method,': 0.07; 'parameter': 0.07; 'python': 0.09; '__future__': 0.09; '__init__': 0.09; 'defined.': 0.09; 'parameter.': 0.09; 'snippet': 0.09; 'zero.': 0.09; 'def': 0.10; 'assigns': 0.16; 'attributes,': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'parameter,': 0.16; 'skip:{ 30': 0.16; 'subject:program': 0.16; 'string': 0.17; 'wrote:': 0.17; 'instance': 0.17; 'passes': 0.17; 'variable': 0.20; 'define': 0.20; 'parameters': 0.20; 'trying': 0.21; 'bit': 0.21; 'import': 0.21; 'assignment': 0.22; 'explicit': 0.22; 'class.': 0.23; 'player': 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'run': 0.28; 'initialized': 0.29; 'definition': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'usually': 0.30; 'says': 0.33; 'int': 0.33; 'to:addr:python-list': 0.33; 'me?': 0.33; 'typically': 0.33; 'skip:- 20': 0.34; 'self': 0.34; 'done': 0.34; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'message-id:@gmail.com': 0.36; 'programmers': 0.36; 'method': 0.36; 'anything': 0.36; 'subject:with': 0.36; 'skip:p 20': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'help': 0.40; 'your': 0.60; 'most': 0.61; 'first': 0.61; 'kind': 0.61; 'referred': 0.62; 'here': 0.65; 'to,': 0.65; 'subject: ': 0.66; 'score': 0.75; 'working,': 0.84; 'do:': 0.91
X-Virus-Scanned amavisd-new at torriefamily.org
Date Sun, 17 Feb 2013 20:49:53 -0700
From Michael Torrie <torriem@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130105 Thunderbird/10.0.12
MIME-Version 1.0
To python-list@python.org
Subject Re: Struggling with program
References <5d90a61d-f122-4dd4-8d79-c9909662b2bc@googlegroups.com>
In-Reply-To <5d90a61d-f122-4dd4-8d79-c9909662b2bc@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
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.1920.1361159410.2939.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361159410 news.xs4all.nl 6904 [2001:888:2000:d::a6]:58520
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39064

Show key headers only | View raw


On 02/17/2013 07:29 PM, maiden129 wrote:
> I'm trying to do this assignment and it not working, I don't
> understand why...
> 
> This is what I have to do:
> 
> 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. can someone please
> help me?

While no one here is willing to do homework for someone else we do like
to help out however we can.

As Dave alluded to, python "instance variables" are called attributes
and typically are initialized in the the __init__(self, ...) method of
the class.  The "self" name, however, can be anything you want.  For
consistency, most python programmers use "self."

So your assignment is a bit misleading when it says a constructor is not
required, because if you want to initialize some instance attributes, it
has to be done in a method, usually the constructor, which in Python is
__init__.

"Instance variables" always need to be referred to with an explicit self
parameter.  This is always the first parameter of any method
declaration. Thus you would define things kind of like so:

---------------------
from __future__ import print_function #in case python2

class MyClass(object):
    def __init__(self, var2):
	# create instance attributes and assign them values
        self.instance_variable = 5
        self.instance_variable2 = var2

    def print_values(self):
        print("instance_variable is {0}, and instance_variable2 is
{1}".format(self.instance_variable, self.instance_variable2))

if __name__ == "__main__":
    c = MyClass(10); # this passes 10 to the __init__ method
    c.print_values() # note there is no self parameter passed;
                     # python passes "c" for you implicitly.
----------------------

If you run this python snippet you get this as output:

instance_variable is 5, and instance_variable2 is 10

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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