我试图通过几个函数传递变量,并在最终函数上我希望得到原始变量的名称.但看起来R中的替代功能只能在"本地"环境中看到,或者只是在一个级别上升.好吧,让我用代码解释一下:
fun1 <- function (some_variable) {deparse(substitute(some_variable)}
fun2 <- function (var_pass) { fun1 (var_pass) }
my_var <- c(1,2) # I want to get 'my_var' in the end
fun2 (my_var) # > "var_pass"
Run Code Online (Sandbox Code Playgroud)
好吧,看起来我们打印的变量名只传递给fun1.替代品的文档告诉我们,我们可以使用env参数来指定我们可以看到的位置.但是,通过传递.全球或.BaseNamespaceEnv作为参数来代替我更奇怪的结果- "some_variable"
我相信这个函数的回答是使用env参数,所以,请你解释一下它是如何工作的,我怎样才能得到我需要的东西.提前致谢!
我只是想知道,下两个函数之间有什么区别(Python 3.x)
def first_example ():
counter = 0
def inc_counter ():
counter += 1
for i in range (10):
inc_counter ()
def second_example ():
counter = [0]
def inc_counter ():
counter[0] += 1
for i in range (10):
inc_counter ()
Run Code Online (Sandbox Code Playgroud)
关于赋值之前引用的第一个函数抛出异常,但第二个函数运行良好.有人可以解释一下,为什么python会记住数组,而不是整数?