Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'value,': 0.03; 'argument': 0.04; '(so': 0.07; 'arguments': 0.07; 'python': 0.09; '__init__': 0.09; 'none.': 0.09; 'self.data': 0.09; 'thrown': 0.09; 'typeerror:': 0.09; 'variables,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'value.': 0.15; 'add(self,': 0.16; 'add.': 0.16; 'given)': 0.16; 'none).': 0.16; 'prog': 0.16; 'received:192.168.11': 0.16; 'wording': 0.16; 'string': 0.17; 'wrote:': 0.17; 'all,': 0.21; 'trying': 0.21; 'cc:2**0': 0.23; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; '(most': 0.27; 'am,': 0.27; 'separate': 0.27; '???': 0.27; 'object,': 0.27; "doesn't": 0.28; 'implies': 0.29; 'parameters.': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'classes': 0.30; 'error': 0.30; 'figure': 0.30; 'file': 0.32; 'print': 0.32; 'traceback': 0.33; 'another': 0.33; 'self': 0.34; 'problem,': 0.35; 'but': 0.36; 'totally': 0.36; 'should': 0.36; 'problems': 0.36; 'display': 0.36; 'does': 0.37; 'two': 0.37; 'why': 0.37; 'passed': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'easier': 0.38; 'things': 0.38; 'gives': 0.39; 'takes': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'save': 0.61; "you'll": 0.62; 'between': 0.63; 'show': 0.63; 'other.': 0.64; 'therefore': 0.65; 'header:Reply-To:1': 0.68; 'programme': 0.69; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'confusing': 0.84; 'received:74.208.4.194': 0.84; 'x):': 0.84 Date: Wed, 24 Oct 2012 08:55:28 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1 MIME-Version: 1.0 To: inshu chauhan Subject: Re: classes References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:XZjS6job/MoU7XAS6FhxNcUE++srH5bSzvUkdELe4Lo 7SqrRIL5VoNwVvZIScq5hpwYGYjtbRVInMEiWc9CaeFmB+K4sS c8kE4rFr2YeBfjwcrOQOydvY1IF4TVE+3EGZM5lD46DzUhF3dp Gt2M6jokEZwn8EXZZ2L7UNrCg9cOfGxiJkT+k+YIS6ujnnphd4 80fOl8lkarFUOje/Hyl/3ZhcBZp3RTGEkx/zPovF/u7iUrH2wH 8S89Z2Fv3/W5n1dJwDD2u7lNSHC+LvXSzMoQzBQb8vAMu4tdx0 DnICxSoeL5zMsCbt3nXRSUd7BHMDl1R10B0zhNxv86zQQm+kvY lqHmSLRKOVnLB4+QSB68= Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351083333 news.xs4all.nl 6946 [2001:888:2000:d::a6]:49158 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32036 On 10/24/2012 08:11 AM, inshu chauhan wrote: > I was just trying out a programme for learning classes in python > > The prog below is showing an error which it should not show : > > class Bag: > def __init__(self, x): > self.data = [] > > def add(self, x): > self.data.append(x) > def addtwice(self, x): > self.add(x) > self.add(x) > y = Bag(4) > print " Adding twice of %4.2f gives " % (y.addtwice()) > Perhaps you're confusing the two x parameters. They are totally independent local variables, and the value of one is not in any way "remembered" for the other. When you want to save things between multiple methods of an object, then store them in the object, perhaps in self.data As it stands, the __init__ does not save the x at all, so the 4 that's passed into the initializer is thrown away. You call addtwice(), but don't supply any value to add. y serves as the self value, but you have no x value. What value did you intend to add? You'll have another problem, in that addtwice() doesn't return any value (so it returns None). Therefore the print isn't going to work. Please separate the print from the calculations, and the problems will be lots easier to figure out. The wording of the string implies it's going to display two values, but the only value given it is None. > Error is : > > Traceback (most recent call last): > File "Z:\learning Python\learn5.py", line 35, in > print " Adding twice of %4.2f gives " % (y.addtwice()) > TypeError: addtwice() takes exactly 2 arguments (1 given) > > why the prog is having this error with self nd x as arguments ??? > > self and x are parameters. You don't pass an argument for x to be bound to. -- DaveA