Bitten by Python scoping

Yet again, I wasted too many minutes staring at and debugging my Python code due to the language’s funky variable scoping:

def relevant(xs, y):
  "Return elements in xs that are relevant to y."
  pairs = ((x, relevance(x,y)) for x in xs)
  return [(x,y) for x,y in pairs if y > 0]

In this case, the y in the list comprehension modifies the binding used by the generator expression.

Follow me on Twitter for stuff far more interesting than what I blog.

  • I feel I should be able to figure out what this does…. but just gave up.
    I think I’m not familiar enough with lambda’s (but perhaps that is a good thing if they’re buggy, as it means I seldom use them)

  • I feel I should be able to figure out what this does…. but just gave up.
    I think I’m not familiar enough with lambda’s (but perhaps that is a good thing if they’re buggy, as it means I seldom use them)

  • Yeah, my example could have been a lot simpler to demonstrate my point; I had simply copied some lines straight from my actual code. I’ve replaced the example with a brand-new one that more clearly illustrates the issue, but is hopefully still believable.

    Also, I didn’t mean to suggest that Python was buggy. I reworded my first sentence to clarify that I was tracking down a bug in my own program, which I was attributing to Python’s funky variable scoping rules.

    A practical lesson: when in doubt, just use different variable names, avoiding the potential for problems altogether.

  • Yeah, my example could have been a lot simpler to demonstrate my point; I had simply copied some lines straight from my actual code. I’ve replaced the example with a brand-new one that more clearly illustrates the issue, but is hopefully still believable.

    Also, I didn’t mean to suggest that Python was buggy. I reworded my first sentence to clarify that I was tracking down a bug in my own program, which I was attributing to Python’s funky variable scoping rules.

    A practical lesson: when in doubt, just use different variable names, avoiding the potential for problems altogether.

  • python 3 already fixed this problem, list comprehension will not modifies the binding anymore in python 3.

  • That is true, I have also come on that front lately!

  • Congatulation! You made this) Hope you'll improve your programming skills and won't waste much time next time as time is money)
    Regards,
    Ann

  • my example could have been a lot simpler to demonstrate my point; I had simply copied some lines straight from my actual code. I've replaced the example with a brand-new one that more clearly illustrates the issue, but is hopefully still believable.

  • ya, I heard ython 3 already fixed this problem.

  • This is the main reason I recruited a developer as my websites codes are in python. In the beginning I tried to troubleshoot everything myself, but later I felt that I was missing many things and good times because of this, so I went for the recruiting option.

  •  Protect you home with  Sump Pump Battery BackupSystem.

  • I had a not dissimilar problem myself when writing html code on a website (http://www.thesecure-store.com/) by trying to copy the example another helpful blogger had posted on favicons. I finally got it to work but need to get an expert in to clean up the code as Google anlaytics do not like the superfluous coding I had managed to write.

  • I use Python somewhat continually, and overall I consider it to be a very outstanding terminology. However, no any terminology is perfect. Here are the disadvantages to be able of significance to me individually like it’s very slowly, losing a few functions that can be useful to common value, etc.