Default scope of variables

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jul 8 07:00:48 CEST 2013


On Mon, 08 Jul 2013 13:11:37 +1000, Chris Angelico wrote:

> On Mon, Jul 8, 2013 at 12:23 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> On Mon, 08 Jul 2013 10:48:03 +1000, Chris Angelico wrote: [...]
>>> That means that I, as programmer, have to keep track of the nesting
>>> level of subtransactions. Extremely ugly. A line of code can't be
>>> moved around without first checking which transaction object to work
>>> with.
>>
>> I feel your pain, but I wonder why we sometimes accept "a line of code
>> can't be moved around" as an issue to be solved by the language. After
>> all, in general most lines of code can't be moved around.
> 
> It's not something to be solved by the language, but it's often
> something to be solved by the program's design. Two lines of code that
> achieve the same goal should normally look the same. This is why
> Python's policy is "one obvious way to do something" rather than "spell
> it five different ways in the same file to make a nightmare for other
> people coming after you". Why should database queries be spelled
> "trans1.query()" in one place, and "trans2.query()" in another?

Is that a trick question? They probably shouldn't. But it's a big leap 
from that to "...and therefore `for` and `while` should introduce their 
own scope".


> Similarly, if I want to call another function and that function needs to
> use the database, why should I pass it trans3 and have that come out as
> trans1 on the other side? Unnecessarily confusing. Makes much more sense
> to use the same name everywhere.

"Is your name not Bruce? That's going to cause a little confusion."

http://www.youtube.com/watch?v=_f_p0CgPeyA



-- 
Steven



More information about the Python-list mailing list