Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python': 0.09; '[1,': 0.09; 'self.data': 0.09; 'types:': 0.09; 'def': 0.10; 'times,': 0.13; 'useful,': 0.13; '"does': 0.16; '[and': 0.16; 'add(self,': 0.16; 'argument.': 0.16; 'brilliant': 0.16; 'pop()': 0.16; 'two,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'changes': 0.20; 'written': 0.20; 'stick': 0.22; 'example': 0.23; 'header:In-Reply-To:1': 0.25; 'possible,': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; '"do': 0.29; 'appending': 0.29; 'style.': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'received:209.85.210.174': 0.30; 'error': 0.30; 'print': 0.32; 'to:addr:python-list': 0.33; 'changed': 0.34; 'received:google.com': 0.34; 'done': 0.34; 'built-in': 0.35; 'returning': 0.35; 'doing': 0.35; 'sometimes': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'method': 0.36; 'useful': 0.36; 'anything': 0.36; 'october': 0.37; 'passed': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'mean': 0.38; 'object': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'back': 0.62; 'necessarily': 0.63; 'legal': 0.65; 'programme': 0.69; 'x):': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:x-originating-ip:in-reply-to:references:from :date:x-google-sender-auth:message-id:subject:to:content-type :x-gm-message-state; bh=+8nCfnMCX99yelLtFsMBqdtjGEvZZXMNV9jiOWAX7jw=; b=aR1072naWKqIOKU0GQr271JTFjy1jhDNiJqUV9EMfhUaeerAA6PmSgsuhXWYuzvV+d yQ45VkRUU/gtV0qfBjkqp8FtBqk/SFZ7sXtxAIeQi6D52tB0ruPcrCFiLDpdGZ8UCUrz WZShpIDHYmWQxRlQWKlfPpgY9KlSeiMFWKWUW9aRjZz7dmV9fMb7vJx6dnctEYyOE6W9 eV7N9IqPfHtH8iXb1SneVnURwTePyvZ9TvMY2pgwB/PftKsCuyy8GhoD1KTtNeUTXpUI FAxJ5pK7/CEp/cC0Ek8TVyGeSTRBwyxz5MEENJgMrN4xzD+4HVsuY7t3RkDTlL+yX4Pr 2Ivg== MIME-Version: 1.0 Sender: z@etiol.net X-Originating-IP: [190.104.29.21] In-Reply-To: References: <5087E540.8060504@davea.name> From: Zero Piraeus Date: Wed, 24 Oct 2012 11:14:26 -0400 X-Google-Sender-Auth: CEB8YyJOoW-nqBl3ywWtZOTPDy4 Subject: Re: classes To: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-Gm-Message-State: ALoCoQl2WqYI55b4MMHGNr3z6yVwD8wQfLCXgbXsP7DvIgnN5Bwgh53CyawJmP2XbLiVQuYLWQ0N X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351091690 news.xs4all.nl 6845 [2001:888:2000:d::a6]:40884 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32050 : On 24 October 2012 09:02, inshu chauhan wrote: > I changed the programme to this : > class Bag: > def __init__(self): > > self.data = [] > > def add(self, x): > self.data.append(x) > def addtwice(self, x): > self.add(x) > self.add(x) > return x > y = Bag() > print y.addtwice(4) > > Now its not showing any error but result is same as the number passed for > adding twice .... That's because, although Bag.addtwice() is appending x to self.data a couple of times, it isn't changing x - so return x will just give back what you supplied as an argument. If you'd written return self.data instead [or just done 'print y.data' after calling addtwice()], you'd see that something has in fact happened to y. By the way ... while Bag.addtwice() is legal Python [and I understand that you're just playing around here], a method that both "does something" [changes the object] and "gives something" [returns a useful value] when that's not strictly necessary isn't brilliant style. Consider a couple of methods on built-in Python types: >>> a = [4, 1, 3, 2] >>> a.sort() # doesn't return anything useful, but ... >>> a # ... changes 'a': [1, 2, 3, 4] >>> b = "one, two, three, four" >>> b.title() # returns something useful ... 'One, Two, Three, Four' >>> b # ... but doesn't change 'b': 'one, two, three, four' >>> A good rule for methods is "do one thing well". Sometimes doing that one thing will necessarily mean both changing the object and returning something - as in the pop() method on lists, for example - but where possible, it's better to stick to one of "doing" or "giving". -[]z.