Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:error': 0.03; 'languages,': 0.04; 'syntax': 0.04; 'value,': 0.04; 'interpreter': 0.05; 'messages.': 0.05; 'attribute': 0.07; 'friend.': 0.07; 'none,': 0.07; 'string': 0.09; '[1,': 0.09; 'append': 0.09; 'assuming': 0.09; 'modifies': 0.09; 'python': 0.11; 'random': 0.14; 'changes': 0.15; '"python': 0.16; "'mylist'": 0.16; 'argument,': 0.16; 'be:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'googling': 0.16; 'interpreter,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'mylist': 0.16; 'none.': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'language': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; "python's": 0.19; 'result.': 0.19; 'thu,': 0.19; 'not,': 0.20; '>>>': 0.22; 'programming': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'example.': 0.24; 'instead.': 0.24; 'tells': 0.24; 'define': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'errors': 0.30; 'especially': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'you?': 0.31; '"",': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; '(most': 0.33; 'checking': 0.33; "can't": 0.35; 'except': 0.35; 'something': 0.35; 'received:84': 0.35; 'but': 0.35; 'there': 0.35; 'right?': 0.36; 'skip:> 10': 0.36; "didn't": 0.36; 'method': 0.36; 'useful': 0.36; 'should': 0.36; 'list': 0.37; 'clear': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'that,': 0.38; 'anything': 0.39; 'recent': 0.39; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'most': 0.60; 'tell': 0.60; 'subject: ': 0.61; "you're": 0.61; 'first': 0.61; 'you.': 0.62; 'information': 0.63; 'name': 0.63; 'soon.': 0.71; 'special': 0.74; 'desperate': 0.84; 'experiment': 0.84; 'learn.': 0.84; 'forgotten': 0.91; 'subject:Lists': 0.91; 'this!': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=ceBcrxzM c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=aoAcgTy3oA8A:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=kZ7UWmmPAAAA:8 a=0xJ7T1x55bOFunWg954A:9 a=QEXdDO2ut3YA:10 a=pyH5b1fOeEsA:10 X-AUTH: mrabarnett:2500 Date: Thu, 23 Oct 2014 02:31:57 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: I am out of trial and error again Lists References: <54484635$0$12992$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 81 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414027919 news.xs4all.nl 2871 [2001:888:2000:d::a6]:53347 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79492 On 2014-10-23 01:10, Seymore4Head wrote: > On Thu, 23 Oct 2014 11:05:08 +1100, Steven D'Aprano > wrote: > >>Seymore4Head wrote: >> >>> Those string errors were desperate attempts to fix the "append" error >>> I didn't understand. >> >>Ah, the good ol' "make random changes to the code until the error goes away" >>technique. You know that it never works, right? >> >>Start by *reading the error message*, assuming you're getting an error >>message. I'm the first person to admit that Python's error messages are not >>always as clear as they should be, especially syntax errors, but still >>there is a lot of information that can be gleamed from most error messages. >>Take this attempt to use append: >> >>py> mylist.append(23) >>Traceback (most recent call last): >> File "", line 1, in >>NameError: name 'mylist' is not defined >> >>That tells me that I have forgotten to define a variable mylist. So I fix >>that: >> >>py> mylist = 23 >>py> mylist.append(23) >>Traceback (most recent call last): >> File "", line 1, in >>AttributeError: 'int' object has no attribute 'append' >> >> >>That tells me that I can't append to a int. After googling for "Python >>append" I learn that I can append to a list, so I try again: >> >>py> mylist = [] >>py> mylist.append(23) >>py> print(mylist) >>[23] >> >> >>Success! >> >>If you are familiar with other programming languages, it might help to think >>of append() as being like a procedure in Pascal, for example. You call >>append() with an argument, but don't expect a return result. >> >>Technically, *all* functions and methods in Python return something, even if >>just the special value None, which can lead to "Gotchas!" like this one: >> >>py> mylist = mylist.append(42) # Don't do this! >>py> print(mylist) # I expect [23, 42] but get None instead. >>None >> >>Oops. One of the small annoyances of Python is that there is no way to tell >>ahead of time, except by reading the documentation, whether something is a >>proper function that returns a useful value, or a procedure-like function >>that returns None. That's just something you have to learn. >> >>The interactive interpreter is your friend. Learn to experiment at the >>interactive interpreter -- you do know how to do that, don't you? If not, >>ask. At the interactive interpreter, if a function or method returns a >>value, it will be printed, *except for None*. So a function that doesn't >>print anything might be procedure-like, and one which does print something >>might not be: >> >>py> mylist = [1, 5, 2, 6, 4, 3] >>py> sorted(mylist) # proper function returns a value >>[1, 2, 3, 4, 5, 6] >>py> mylist.sort() # procedure-like function returns None >>py> print(mylist) # and modifies the list in place >>[1, 2, 3, 4, 5, 6] > > I am going to get around to learning the interpreter soon. > Why wait? You're trying to learn the language _now_, and checking things interactively will help you.