scope

Python nested functions variable scoping [duplicate]

允我心安 提交于 2019-12-17 03:30:14
问题 This question already has answers here : UnboundLocalError with nested function scopes (4 answers) Closed last year . I've read almost all the other questions about the topic, but my code still doesn't work. I think I'm missing something about python variable scope. Here is my code: PRICE_RANGES = { 64:(25, 0.35), 32:(13, 0.40), 16:(7, 0.45), 8:(4, 0.5) } def get_order_total(quantity): global PRICE_RANGES _total = 0 _i = PRICE_RANGES.iterkeys() def recurse(_i): try: key = _i.next() if

Controlling the value of 'this' in a jQuery event

眉间皱痕 提交于 2019-12-17 02:45:08
问题 I have created a 'control' using jQuery and used jQuery.extend to assist in making it as OO as possible. During the initialisation of my control I wire up various click events like so jQuery('#available input', this.controlDiv).bind('click', this, this.availableCategoryClick); Notice that I am pasing 'this' as the data argument in the bind method. I do this so that I can get at data attached to the control instance rather from the element that fires the click event. This works perfectly,

Accessing outside variable using anonymous function as params

走远了吗. 提交于 2019-12-17 02:35:16
问题 Basically I use this handy function to processing db rows (close an eye on PDO and/or other stuff) function fetch($query,$func) { $query = mysql_query($query); while($r = mysql_fetch_assoc($query)) { $func($r); } } With this function I can simply do: fetch("SELECT title FROM tbl", function($r){ //> $r['title'] contains the title }); Let's say now I need to concatenate all $r['title'] in a var (this is just an example). How could I do that? I was thinking something like this, but it's not very

Angular $scope.$apply vs $timeout as a safe $apply

风流意气都作罢 提交于 2019-12-17 02:11:53
问题 I'm trying to better understand the nuances of using the $timeout service in Angular as a sort of "safe $apply" method. Basically in scenarios where a piece of code could run in response to either an Angular event or a non-angular event such as jQuery or some standard DOM event. As I understand things: Wrapping code in $scope.$apply works fine for scenarios where you aren't already in a digest loop (aka. jQuery event) but will raise an error if a digest is in progress Wrapping code in a

Angular $scope.$apply vs $timeout as a safe $apply

风格不统一 提交于 2019-12-17 02:11:17
问题 I'm trying to better understand the nuances of using the $timeout service in Angular as a sort of "safe $apply" method. Basically in scenarios where a piece of code could run in response to either an Angular event or a non-angular event such as jQuery or some standard DOM event. As I understand things: Wrapping code in $scope.$apply works fine for scenarios where you aren't already in a digest loop (aka. jQuery event) but will raise an error if a digest is in progress Wrapping code in a

Error: 'else' without 'if'

≯℡__Kan透↙ 提交于 2019-12-16 22:16:48
问题 Getting an else without if statement: import java.util.Scanner; public class LazyDaysCamp { public static void main (String[] args) { int temp; Scanner scan = new Scanner(System.in); System.out.println ("What's the current temperature?"); temp = scan.nextInt(); if (temp > 95 || temp < 20); System.out.println ("Visit our shops"); else if (temp <= 95) if (temp >= 80) System.out.println ("Swimming"); else if (temp >=60) if (temp <= 80) System.out.println ("Tennis"); else if (temp >= 40) if (temp

Surprised that global variable has undefined value in JavaScript

两盒软妹~` 提交于 2019-12-16 19:33:25
问题 Today, I got completely surprised when I saw that a global variable has undefined value in certain case. Example: var value = 10; function test() { //A console.log(value); var value = 20; //B console.log(value); } test(); Gives output as undefined 20 Here, why is the JavaScript engine considering global value as undefined . I know that JavaScript is an interpreted language. How is it able to consider variables in the function? Is that a pitfall from the JavaScript engine? 回答1: This phenomenon

How to access private variable of Python module from class

半城伤御伤魂 提交于 2019-12-14 03:59:54
问题 In Python 3, prefixing a class variable makes it private my mangling the name within the class. How do I access a module variable within a class? For example, the following two ways do not work: __a = 3 class B: def __init__(self): self.a = __a b = B() results in: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in __init__ NameError: name '_B__a' is not defined Using global does not help either: __a = 3 class B: def __init__(self): global __a

ColdFusion 11: Changing Application “this” Scope metadata from different functions in extended Application.cfc

北慕城南 提交于 2019-12-14 03:53:26
问题 I have a general understanding of the ColdFusion Application Workflow. But some parts remain unclear to me. The general workflow is as follows... Public Application Settings are set (I.E this.name, this.mappings ext.) onApplicationStart onSessionStart onRequestStart I have a sub-directory with an Application.cfc that extends a root Application.cfc that exists at the web-root After reading tons of blogs that describe this design pattern.... ColdFusion Application-Specific Mappings Work With

Post-Redirect-Get when passing data to the form?

戏子无情 提交于 2019-12-14 03:44:05
问题 I have several scenarios where a servlet needs to pass data to a form in a JSP from the retrieved records from the database. Currently, I'm storing this information in the request, using a RequestDispatcher to forward to the page, and all is well. However, this is not fitting with the PRG pattern (AFAIK), and of course means that refreshing of the resulting JSP means re-running of the servlet, which is undesirable. I could of course store these values in the session, but that would mean