Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'represents': 0.05; 'attribute': 0.07; 'subject:code': 0.07; 'attributes': 0.09; 'logic': 0.09; 'oop': 0.09; 'screen.': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'ball.': 0.16; 'balls': 0.16; 'bouncing': 0.16; 'exist.': 0.16; 'loop.': 0.16; 'settings,': 0.16; 'similarly,': 0.16; 'subject:OOP': 0.16; 'y):': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; '<': 0.19; 'advance.': 0.19; 'settings': 0.22; 'email addr:gmail.com>': 0.22; 'separate': 0.22; 'error': 0.23; '>': 0.26; 'header:In- Reply-To:1': 0.27; 'appreciated.': 0.29; 'am,': 0.29; 'related': 0.29; "doesn't": 0.30; 'errors': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'that.': 0.31; 'ball': 0.31; 'class': 0.32; 'probably': 0.32; 'screen': 0.34; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'received:google.com': 0.35; '(e.g.,': 0.36; 'controls': 0.36; 'method': 0.36; 'thanks': 0.36; 'should': 0.36; 'skip:& 10': 0.38; 'window': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'skip:& 20': 0.39; 'does': 0.39; 'use.': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'new': 0.61; 'name': 0.63; 'details': 0.65; 'here': 0.66; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=+pSE+HqsCSgyff0pdaDyW56TT0ap054rwmrs8Rxw0dk=; b=NI+Wgu2J30q/PmiAh6eTTxfoVkZ28FOlshsoxAvrLqVpGExhu3zONZO/F2+p+50lb1 Tkh+0jOBxr6fQQgw+caFdy6Bcij3OyqsuIu8OWEt24L1bgeAsNFzXQ5j25iHCgofNqRl FPwyZAUaaiTiiTX3fFhQBhnC6GjdWDLI8pFReqKfi1fUZD9GGIyHXTQ0+VKUsGIAMsSc igLKu/NXUvkV7RxkonEiLlVz+kspRJZBN640vW+ELRYk3ktlirIQxykyiEsMjKkL943/ QvLToClktx3S3CNF1dcKWBocx4TfP9P3ROq33AHHBH6xn5VvPJqbsPl4rgSHpn9BKjWW GqhQ== MIME-Version: 1.0 X-Received: by 10.107.136.89 with SMTP id k86mr5750750iod.63.1431102148068; Fri, 08 May 2015 09:22:28 -0700 (PDT) In-Reply-To: <009ceef8-066d-4d92-a6c9-761e86584e75@googlegroups.com> References: <009ceef8-066d-4d92-a6c9-761e86584e75@googlegroups.com> Date: Fri, 8 May 2015 10:22:28 -0600 Subject: Re: How to properly apply OOP in the bouncing ball code From: Ian Kelly To: Python Content-Type: multipart/alternative; boundary=001a1145a0722c534c0515946f83 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431102150 news.xs4all.nl 2962 [2001:888:2000:d::a6]:58211 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90269 --001a1145a0722c534c0515946f83 Content-Type: text/plain; charset=UTF-8 On May 8, 2015 9:46 AM, "Tommy C" wrote: > > I'm trying to apply OOP in this bouncing ball code in order to have multiple balls bouncing around the screen. The objective of this code is to create a method called settings, which controls all the settings for the screen and the bouncing behaviour of multiple balls, under the class Ball. However, I keep on getting errors related to attributes (e.g., speed). I'm new to OOP in Python so your help will be much appreciated. Thanks in advance. As the error says, the attribute does not exist. > class Ball: > def __init__(self, X, Y): > self.velocity = [1,1] Here where you set it, you call it "velocity". > speed = self.velocity Here you create a local variable called "speed", which you never use. > if balls.ball_boundary.left < 0 or balls.ball_boundary.right > self.width: > balls.speed[0] = -balls.speed[0] And here you look up an attribute of Ball called "speed", which doesn't match the name you used in __init__. This is a muddled design overall. Your Ball class represents the individual balls bouncing around the screen. It should not also contain details about window size and the logic for the event loop. Use a separate class for that. Similarly, if the purpose of your settings method is to manage settings, why does it also contain all the bouncing logic? These should probably be separate methods. --001a1145a0722c534c0515946f83 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On May 8, 2015 9:46 AM, "Tommy C" <tommyc168168@gmail.com> wrote:
>
> I'm trying to apply OOP in this bouncing ball code in order to hav= e multiple balls bouncing around the screen. The objective of this code is = to create a method called settings, which controls all the settings for the= screen and the bouncing behaviour of multiple balls, under the class Ball.= However, I keep on getting errors related to attributes (e.g., speed). I&#= 39;m new to OOP in Python so your help will be much appreciated. Thanks in = advance.

As the error says, the attribute does not exist.

> class Ball:
> =C2=A0 =C2=A0 def __init__(self, X, Y):
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.velocity =3D [1,1]

Here where you set it, you call it "velocity".

> =C2=A0 =C2=A0 =C2=A0 =C2=A0 speed =3D self.velocity

Here you create a local variable called "speed", w= hich you never use.

> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= if balls.ball_boundary.left < 0 or balls.ball_boundary.right > self.= width:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = balls.speed[0] =3D -balls.speed[0]

And here you look up an attribute of Ball called "speed= ", which doesn't match the name you used in __init__.

This is a muddled design overall. Your Ball class represents= the individual balls bouncing around the screen. It should not also contai= n details about window size and the logic for the event loop. Use a separat= e class for that.

Similarly, if the purpose of your settings method is to mana= ge settings, why does it also contain all the bouncing logic? These should = probably be separate methods.

--001a1145a0722c534c0515946f83--