[Tutor] Function works one time then subsequently fails

Cameron Simpson cs at zip.com.au
Wed Apr 29 07:40:03 CEST 2015


On 28Apr2015 22:27, Jim Mooney Py3winXP <cybervigilante at gmail.com> wrote:
>On 28 April 2015 at 21:27, Cameron Simpson <cs at zip.com.au> wrote:
>> At a first glance numbers is a global. It is reset to [] at program start,
>> but never again. So you're appending to it forever. I have not investigated
>> further as to how that affects your program's flow.
>
>Took you less time to find that than me. I, of course, realized I forgot to
>empty the number list on each time through the parse loop, After I posted
>;')  Seems to work okay otherwise, although criticism of Pythonicity is
>welcome. I just wanted to practice tossing functions around. I'll put it
>into a user input loop and work it. It won't catch everything. I have to
>study regexes for that.

Basicly, you should almost never have a global variable. There is a host of 
special circumstances where globals solve some specific problems. But by and 
large, don't do it without a very concrete reason.

If numbers had been local to the parse function you would never have been 
bitten by this. There's no need for it to be global; you can return the 
operator and the operands (numbers) easily from the parse function.

>At this point I'm starting to lose track and have to think of better ways
>to organize so I recall and understand what I'm doing. I know there are
>general tuts on that but that's just reading. Is there a python-specific
>tut on it where I could follow actual code along with the interpreter to
>reinforce things? Or, what is the official word for
>organize-so-you-don't-forget-or-get-confused so I can search google with it?

"functional decomposition"?

One rule of thumb is that functions should be short. That way you can generally 
keep their purpose and internal oparations in your head in one go, and the 
whole thing also fits on your screen.

As with all things, sometimes that cannot be reasonably achieved, but it is 
usually so.

We can pick over your code as well if you like. Should we?

Cheers,
Cameron Simpson <cs at zip.com.au>

If this experiment we're doing works, then I will follow up and push it as
hard as possible. And if it doesn't work, I will write a science-fiction
novel where it does work. It's a win-win situation.
- John Cramer on his experiment for possible cuasality violation


More information about the Tutor mailing list