[{"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292619.862571, "message": "that makes it clearer :)", "group_id": 292, "id": 367339}, {"user_id": 21093, "stars": [{"date_created": 1300292255.7076111, "user_id": 3580}, {"date_created": 1300424863.740751, "user_id": 214}], "topic_id": 13368, "date_created": 1300292017.5232079, "message": "Change the nested def to 'def func(w=w)' and it solves it. The issue is you're not referencing w until after the loop and it has been reset to the last value.", "group_id": 292, "id": 367278}, {"user_id": 21093, "stars": [], "topic_id": 13368, "date_created": 1300292590.6821809, "message": "Not sure if I'm making this any clearer ;-)", "group_id": 292, "id": 367329}, {"user_id": 21093, "stars": [], "topic_id": 13368, "date_created": 1300292635.092551, "message": "Yep Java != Python -- this is actually an extremely useful feature in Python", "group_id": 292, "id": 367340}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300291563.0276999, "message": "the output of that will be:", "group_id": 292, "id": 367216}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300291617.8143981, "message": "I know I could rewrite this with lambda, but my real use case involves an implementation of func() that takes more than one argument", "group_id": 292, "id": 367225}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300291585.9642889, "message": "intuitively I'm imagining the for loop creating a new scope, a new func created, and a new reference (bound to my new func) getting added to the list.. but instead I guess I've added to result 4 references to _locals()['func'], and the last impl wins.", "group_id": 292, "id": 367218}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300291454.564224, "message": "I think I've dealt with this once before, but it's escaping me. This is one of the more confusing issues with scope in python that I haven't grokked all that well yet (been a serious python dev < a year)...", "group_id": 292, "id": 367195}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300291559.2338009, "message": "def func_list(*words):\n \n result = []\n for w in words:\n def func():\n print w\n result.append(func)\n \n return result\n\nfor f in func_list('These', 'are', 'my', 'words')\n f()", "group_id": 292, "id": 367214}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300291640.3495131, "message": "So yeah... can someone school me on the write way to think about code like this?", "group_id": 292, "id": 367227}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292657.4715381, "message": "haha. um, thanks for the clarification on the languages not being equivilant :)", "group_id": 292, "id": 367342}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292715.241313, "message": "yeah, I can see how this allows us to do things that my original mental model for it wouldn't make possible at all", "group_id": 292, "id": 367347}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292863.2309051, "message": "many thanks for taking the time to not only answer but help make sure I understand it for good, @sdeibel :)", "group_id": 292, "id": 367372}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300291565.314131, "message": "words\nwords\nwords\nwords\n>>> \n", "group_id": 292, "id": 367217}, {"user_id": 4077, "stars": [], "topic_id": 13368, "date_created": 1300292137.745847, "message": "def func_list(*words):\n\n result = []\n for w in words:\n def func(w=w):\n print w\n result.append(func)\n\n return result\n\nfor f in func_list('These', 'are', 'my', 'words'):\n f()\n", "group_id": 292, "id": 367286}, {"user_id": 4077, "stars": [], "topic_id": 13368, "date_created": 1300292164.435338, "message": "didn't see @sdeibel 's answer, but yeah, needed to reference w", "group_id": 292, "id": 367291}, {"user_id": 21093, "stars": [], "topic_id": 13368, "date_created": 1300292393.029355, "message": "In case it makes it any clearer: the nested def func is using w in the enclosing scope until you add the w=w arg in which case a distinct w in the nested scope is created and bound to the value of the outer w. Also, def's are \"executed\" when reached in that a function object is created and bound args are part of what gets created at that time. Of course the contents of the function are not executed until you invoke it later.", "group_id": 292, "id": 367311}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292262.6304131, "message": "aaaargh. totally", "group_id": 292, "id": 367301}, {"user_id": 21093, "stars": [], "topic_id": 13368, "date_created": 1300292506.5481191, "message": "Also, in case it helps: You end up creating 4 distinct callable function objects in the loop -- so think of those as similar to setting a variable to a value and adding it to a list (in the result.append). Just happens this variable 'func' is set to a callable function object w/ some already-bound args.", "group_id": 292, "id": 367321}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292586.2901499, "message": "and each of the functions (in my original share a reference to w, which belongs to func_list).. mind-melting coming from java where I'd expect a distinct w in the nested scope o the for loop", "group_id": 292, "id": 367328}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292607.8490729, "message": "yeah.. my real use cases outputs a list of comparators.. so I end up defining func(a,b,name=name)", "group_id": 292, "id": 367334}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292612.7645619, "message": " for w in words:\n def func(x=w):\n print x\n", "group_id": 292, "id": 367335}, {"user_id": 21093, "stars": [], "topic_id": 13368, "date_created": 1300292720.6800699, "message": "Yes, I know. I ran into the same confusion myself and even still get bitten by this from time to time since a tend not to do w=w style binding unless it's needed", "group_id": 292, "id": 367349}, {"user_id": 4077, "stars": [], "topic_id": 13368, "date_created": 1300292142.148128, "message": "oh snap", "group_id": 292, "id": 367288}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292364.3441031, "message": "right, so to try to burn this into my brain for permanent.. I'm still trying to understand the scope issues around w here...", "group_id": 292, "id": 367310}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292441.4155979, "message": "and the special part of the w=w is that we've made an assignment?", "group_id": 292, "id": 367314}, {"user_id": 3580, "stars": [], "topic_id": 13368, "date_created": 1300292674.8265569, "message": "they deal with arguments/references very similar.. so it's easy to get off track", "group_id": 292, "id": 367343}, {"user_id": 21093, "stars": [], "topic_id": 13368, "date_created": 1300292578.7829959, "message": "Also note if you had 'def func(x, w=w)' then you would be able to call it later as func(1) (providing the one unbound arg). So there's some flexibility here in combining already-bound args and unbound ones in the callable that is created.", "group_id": 292, "id": 367327}, {"user_id": 21093, "stars": [], "topic_id": 13368, "date_created": 1300292882.840153, "message": "No problem.", "group_id": 292, "id": 367373}]