问题标签 [locals]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
798 浏览

if-statement - Stata - 如果变量名包含在本地,则运行代码

我想if在 Stata 中有一个条件,它只在某个变量的名称包含在local. 例如

在Stata中如何做到这一点?

0 投票
4 回答
2664 浏览

python - 在不直观的函数中调用 locals()?

这可能是基本的,但可以帮助我理解名称空间。一个好的解释可能会逐步介绍函数定义执行时会发生什么,然后是函数对象执行后会发生什么时会发生什么。递归可能会使事情复杂化。

结果对我来说并不明显;我本来期望:

locals_1 将包含 var;locals_2 将包含 var 和 locals_1;locals_3 将包含 var、locals_1 和 locals_2

结果如下:

该模式似乎是,对于 locals 的 (n) 调用所有
返回的 locals-dicts 都是相同的,并且它们都包括第一个 (n-1) 个 locals-dicts。

有人可以解释一下吗?

进一步来说:

为什么 locals_1 包含自己?

为什么 locals_1 包含 locals_2?func时是否分配了 locals_1 是在创建或执行

为什么 locals_3 不包括在任何地方?

“{...}”是否表示“无限递归”?有点像那些镜子面对面的照片?

0 投票
1 回答
87 浏览

python - 在 python 中使用 format(**locals()) 插入值之前对其进行操作

在 .format() 和 **locals() 中使用它之前是否可以在不创建新变量的情况下操作局部变量?所以与此具有相同效果的东西:

但更优雅,无需创建 width2 和 height2 变量。我试过这个:

但它会在 locals() 之后的第一个逗号处引发错误“SyntaxError: invalid syntax”。

0 投票
1 回答
1793 浏览

jquery - Python Bottle - jQuery AJAX success function response is HTML

I am having an issue with Python Bottle and the jQuery AJAX function. Right now, when I try to use the AJAX call after a button submission call (via the jQuery.submit() function), the data that is passed back is the entire HTML text.

On the Bottle side, I have code that looks as such:

On the JS side, I have code that looks as such:

On the HTML side, I have code that looks as such:

My assumption is that when I click the button, it makes the AJAX call, which goes to the Python file via the Bottle interface. In this .py file, it will then make the test() call and return a dictionary of the local values (via locals()). However, when I log into console the 'data' object that is passed back, it turns out to be the entire HTML file (including tags).

My question is: why is this the case? I have figured out that the responseText for the jqXHR object matches the data that is given to the success function callback.

EDIT:

Let me explain the general flow of this. I begin by loading the /testing page. Upon doing so, the test.tpl template file renders fine and the Python file returns the locals() dictionary. However, after this page is loaded, when an AJAX call is fired, I have it go to this same test() Bottle method with a query. In response to this AJAX query, locals() will still return the same data, but the data that will be returned to the JS (the data belonging to the success callback function) will now be the entire HTML text, as opposed to a dictionary.

However, if I were to change this and have a separate return (i.e. return json.dumps(c)), then the data registered will be a JSON file (once I add in the dataType='json' part. So I guess herein lies my confusion. What is different about AJAX that causes this result?

0 投票
1 回答
3206 浏览

python - locals().update(dictionary) 不会添加所有变量

我一直在使用字典对象加载变量,但值得到了更新。我在这里想念什么?

最后一行是我得到一个断言错误。这是怎么回事?

0 投票
2 回答
529 浏览

python - python2.7:在实例方法中传递 kwargs

如果我想将一个相同的 args 列表从一个函数传递到另一个函数,我可以使用 locals 函数。但是,如果我在实例方法中,这就会失败,因为 self 不能作为 kwarg 传递。我可以创建一个本地人的字典,然后删除自己。我想知道是否有更优雅的方式来做这件事——这对我来说似乎很多。

有没有更好的方法将相同的列表参数从 MyVerboseObj.test 传递给 MyObj.test?

** 更新 **

这是一个简化的例子来演示我的问题。一个更实际的例子可能是这样的:

我有很多显示对象,它们具有由不同模块和不同命名约定定义的不同变量——我正在尝试创建具有通用功能的显示对象。

0 投票
2 回答
96 浏览

.net - .Net:当您 dispose() 变量时,是否应该立即在调试器 Locals 窗口中将其设置为“Nothing”?

当您 dispose() 一个变量时,是否应该立即在调试器 Locals 窗口中将其明显设置为“Nothing”?

当我在 Visual Studio 2010 调试器中单步执行此操作时,我可以在 Locals 窗口中看到 close() 调用确实将 r1 的 BaseStream 和各种其他成员设置为 Nothing,但 dispose() 调用不会将 r1 变量设置为整体到“无”。它仍然在 Locals 窗口中列为 {System.IO.StreamReader}。

也许我的期望与它的实际工作方式不匹配。是否有明确的参考来解释这种行为?

编辑:是的,我已经知道 Using 语句,但是我需要分配两个 db 连接和两个 db 命令(每个打开的连接一个)。使用四深的语句嵌套似乎很复杂。此外,我想围绕每个 New Connection() 语句实现一个三击而你退出 while 循环,因为它们在我当前的环境中很容易失败,而我正在专门尝试解决这个问题。如果有人可以为我指出一个有用的结构来混合 While 和 Usings,他们会获得当天的金星。

0 投票
2 回答
858 浏览

python - 按变量声明的顺序检索 locals() - python

例如,

打电话时locals(),我得到

如何按照我定义的顺序从 locals() 检索局部变量?

0 投票
2 回答
86 浏览

python - 使用 locals() 动态分配变量在递归函数中不起作用

我有一个递归脚本,它正在为汽车抓取 JSON 文件。在每个递归级别,它都会添加一个新变量,并将其(与其他值一起)传递给递归调用,每次都会获得越来越详细的信息。我尝试使用locals()动态分配一个变量,但None即使在调用之后它仍然存在(我记得有时看到它locals()是只读的)。

我也尝试过使用eval(),它给了我同样的问题(我知道 eval 并不理想)。理想情况下,我希望避免使用字典,因为这需要我先用值加载它,这似乎有一些不必要的步骤,但我现在对任何事情都持开放态度。

例子:

scraper(manufacturer='Honda')将抓取模型的 JSON 文件,设置model='Accord'然后递归调用

scraper(manufacturer='Honda, model='Accord')它会刮掉一个年份的文件,设置year=2014并递归调用

scraper(manufacturer='Honda', model='Accord', year='2014')这是基本情况

我会很感激有关如何处理这个问题的任何意见,我知道 Python 似乎总是有一些聪明的做事方式,而且我一直在学习更多关于它的知识,所以提前谢谢你!我也在这个例子中使用 Python3,如果这改变了什么

0 投票
1 回答
2330 浏览

emacs - 在 emacs 中设置每个项目的缩进变量

我的 emacs 设置为使用 2 个空格进行全局缩进:

但我想为一个使用 4 个空格进行缩进的 Web 项目(主要是 html、css 和 js)做出贡献。所以我试图.dir-locals.el在项目目录中设置一个文件。该文件具有以下设置(使用add-dir-local-variable命令添加):

但这些设置不会生效。当我在项目子目录中打开 .js 或 .html 文件时,按 Tab 会缩进 2 个空格。

我究竟做错了什么?