假设我想编写一个简单的重命名函数,它可以通过.Rprofile. 该函数很简单,可以比较为:
carsNewName <- mtcars; rm(mtcars)
Run Code Online (Sandbox Code Playgroud)
.Rprofile可用的函数.Rprofile格式为:
.env$rename <- function(oldName, newName) {
newName <- oldName
rm(oldName, envir = parent.env())
return(newName)
}
Run Code Online (Sandbox Code Playgroud)
在哪里。env通过attach(.env).
如何通过 访问函数的父环境parent.env()?即,如果rename在另一个函数内部调用函数,我想重命名不在全局环境中的对象。
我有一个简单的例子:
def func1():
local_var = None
def func(args):
print args,
print "local_var:", local_var
local_var = "local"
func("first")
func("second")
func1()
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
first local_var: None second local_var: local
但是,我的实际输出是:
first local_var:
Traceback (most recent call last):
File "test.py", line 13, in
func1()
File "test.py", line 10, in func1
func("first")
File "test.py", line 6, in func
print "local_var:", local_var
UnboundLocalError: local variable 'local_var' referenced before assignment
我对python范围规则的理解要求它应该按预期工作.我有其他代码,这可以按预期工作,但减少一个非工作代码片段到它上面的小部分情况也不起作用.所以我很难过.
我正在按照教程显示工厂模式以在javascript中创建对象.下面的代码让我难以理解它的工作原理.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>6-2.htm</title>
</head>
<body>
<script type="text/javascript">
function createAddress(street, city, state, zip) {
var obj = new Object();
obj.street = street;
obj.city = city;
obj.state = state;
obj.zip = zip;
obj.showLabel = function() {
//alert(this.street + "\n" + this.city + ", " + this.state + " " + this.zip);
//var obj;
alert(obj.street + "\n" + obj.city + ", " + obj.state + " " + obj.zip);
};
return obj;
};
var JohnAddr = …Run Code Online (Sandbox Code Playgroud) 我想知道为什么在Ocaml中,"让......和......"没有"类型......和......"那样的范围:
下面的一个是好的,t2在与t1相同的范围内
# type t1 = t2
and t2 = int;;
Run Code Online (Sandbox Code Playgroud)
以下是WRONG,v2 in not in the scoping
# let v1 = v2
and v2 = 3;;
Characters 9-11:
let v1 = v2
^^
Error: Unbound value v2
Run Code Online (Sandbox Code Playgroud)
甚至"让rec"也行不通......
# let rec v1 = v2
and v2 = 3;;
Characters 13-15:
let rec v1 = v2
^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
Run Code Online (Sandbox Code Playgroud)
为什么"类型......和"与"让...和"之间的范围不一致?谢谢.
这与我到目前为止所读到的每个其他范围问题几乎相同,this除了一个微小的差异,这使得它(imo)提出这个问题是相关的.
现在我的问题是this使用Knockout和Typescript 确定范围,因此给出以下内容:
class ViewModel
{
public SomeObservableArray = new ko.observableArray();
public AddToTheObservableArray(someJsonData: any)
{
this.SomeObservableArray.push(new SomePojo(someJsonData));
}
}
Run Code Online (Sandbox Code Playgroud)
所以this在上面的代码位将炸毁因为打字稿让你认为this是类的实例,但在现实中却是一些其他的这个,因为Ajax回调或视图的元素,无论是覆盖场景this的关键字.
所以要解决这个问题,大多数解决方案都是将代码移动到类的构造函数中,我个人认为这很糟糕,但是考虑到使用TypeScript带来的其他好处,这种少量的恐怖是可以接受的.所以我们都在同一页面上,下面的代码修复了上述问题:
class ViewModel
{
public SomeObservableArray = new ko.observableArray();
public AddToTheObservableArray = (someJsonData: any) => Void;
constructor
{
this.AddToTheObservableArray = (someJsonData: any) => {
this.SomeObservableArray.push(new SomePojo(someJsonData));
};
}
}
Run Code Online (Sandbox Code Playgroud)
我只是把这个示例代码写在我的头顶上,所以我为任何拼写错误等道歉,但它是所面临的常见问题和常见解决方案/解决方法.
现在!我的问题是从这里开始的下一步,我有这样的代码:
class ViewModel
{
public SomeObservableArray = new ko.observableArray();
public AddToTheObservableArray = (someJsonData: any) => Void;
constructor …Run Code Online (Sandbox Code Playgroud) 当我使用字符串流时,我的cpp程序在使用作用域时做了一些奇怪的事情.当我将字符串和字符串流的初始化放在与我使用它的块相同的块中时,没有问题.但是如果我将它放在上面的一个块中,则字符串流不能正确输出字符串
正确的行为,程序打印由空格分隔的每个标记:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main () {
while (true){
//SAME BLOCK
stringstream line;
string commentOrLine;
string almostToken;
getline(cin,commentOrLine);
if (!cin.good()) {
break;
}
line << commentOrLine;
do{
line >> almostToken;
cout << almostToken << " ";
} while (line);
cout << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
行为不正确,程序只打印第一个输入行:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main () {
//DIFFERENT BLOCK
stringstream line;
string commentOrLine;
string almostToken;
while (true){
getline(cin,commentOrLine);
if …Run Code Online (Sandbox Code Playgroud) 我不明白为什么我这么小的包装函数会产生主题错误.下面应该重现它.我的目标是从单个数据帧中的数据中做一堆图,每个图都位于一个新窗口中.
library(ggplot2)
library(datasets)
data(ChickWeight)
str(ChickWeight)
# This works fine:
qplot(x = weight, y = Time, data = ChickWeight, color = Diet)
myfun <- function(pred = weight, resp = Time, dat = ChickWeight) {
windows()
qplot(x = pred, y = resp, data = dat, color = Diet)
}
# But this returns 'Error in eval(expr, envir, enclos) : object 'weight' not found':
myfun()
# As does this
myfun(weight, Time)
Run Code Online (Sandbox Code Playgroud)
为什么R不能在我的功能中找到'重量'?
我在Windows 8.1 64位上运行R版本3.0.1,64位.
谢谢!
-Roy
我正在尝试a<-get(obj1,envir=parent.environment())从被调用的环境中访问驻留在调用环境中的对象(),但myf我无法使其正常工作.我得到的错误是Object obj1 not found.我parent.frame()也试过了.有任何想法吗?
library(shiny)
shinyApp(
ui = textOutput("test1"),
server = function(input, output) {
myf <- function(x) {
a <- get(obj1, envir = parent.environment())
return(paste0(x,a))
}
output$test1 <- renderText({
obj1 <- "testing"
a <- lapply(c("a","b","c"), myf)
return(paste(unlist(a), collapse = ","))
})
}
)
Run Code Online (Sandbox Code Playgroud)
注意:我不想obj1<<-创建在全局环境中创建的obj1 ,并且可用于所有会话
我尝试了解R您可以访问的范围演示demo(scoping).
我不明白total变量的保存位置.首先,我认为根据help("<<-")
运算符<< - 和 - >>通常仅在函数中使用,并且通过父环境搜索要分配的变量的现有定义.如果找到这样的变量(并且其绑定未被锁定),则重新定义其值,否则在全局环境中进行赋值.
它在global environment.但是因为我在那里找不到它,(ls(environment))我想open.account <- function(total)为由赋值创建的所有实例创建一个总变量open.account().但是,如果我创建一个实例,ross <- open.account(100)我找不到变量.
ross
...
<environment: 0x0000000011fbe998>
Run Code Online (Sandbox Code Playgroud)
与ls(environment(environment: 0x0000000011fbe998)).结果getAnywhere(total)是no object named ‘total’ was found.那么不同版本的生活在哪里total?
我有一个Vue.js组件,该组件的数据变量是JSON对象,并且我已经制作了一种方法,该方法旨在通过键轻松地在该对象中递归查找项目。
这里是:
getJsonItem(lookup, obj=this.cardInfo) {
for (item in obj) {
if (item == lookup) {
return obj[item];
} else if (obj[item] instanceof Object) {
getJsonItem(lookup, obj[item]);
}
}
},
Run Code Online (Sandbox Code Playgroud)
现在,这在Vue JS之外的控制台中工作得很好,但是当我尝试从Vue JS中作为一种方法运行它时,出现“ ReferenceError:未定义项目”错误。
Vue JS中的变量作用域是否发生了一些有趣的事情,使我无法引用在for循环中声明的“ item”变量?
我确定这可能是我没看到的简单事件。