我尝试做的是:
public class History {
public class State {
public enum StateType {
Run Code Online (Sandbox Code Playgroud)
Eclipse给出了我的编译错误StateType
:The member enum StateType must be defined inside a static member type
.
当我使State
类静态时,错误消失.我可以制作State
静态,但我不明白为什么我不能enum
在内部类中声明.
def maker(n):
def action(x):
return x ** n
return action
f = maker(2)
print(f)
print(f(3))
print(f(4))
g = maker(3)
print(g(3))
print(f(3)) # still remembers 2
Run Code Online (Sandbox Code Playgroud)
为什么嵌套函数会记住第一个值,2
即使maker()
已经返回并退出时action()
被调用?
我希望有人可以帮助我使用这个Javascript.
我有一个名为"设置"的对象,我想编写一个向该对象添加新设置的函数.
新设置的名称和值以字符串形式提供.然后,提供设置名称的字符串将下划线拆分为数组.通过使用数组的每个部分给出的名称创建新的嵌套对象,新设置应添加到现有的"设置"对象中,除了最后一部分应该是给出设置值的字符串.然后我应该能够参考设置,例如提醒它的价值.我可以像这样以静态的方式做到这一点......
var Settings = {};
var newSettingName = "Modules_Video_Plugin";
var newSettingValue = "JWPlayer";
var newSettingNameArray = newSettingName.split("_");
Settings[newSettingNameArray[0]] = {};
Settings[newSettingNameArray[0]][newSettingNameArray[1]] = {};
Settings[newSettingNameArray[0]][newSettingNameArray[1]][newSettingNameArray[2]] = newSettingValue;
alert(Settings.Modules.Mediaplayers.Video.Plugin);
Run Code Online (Sandbox Code Playgroud)
...创建嵌套对象的部分正在执行此操作...
Settings["Modules"] = {};
Settings["Modules"]["Video"] = {};
Settings["Modules"]["Video"]["Plugin"] = "JWPlayer";
Run Code Online (Sandbox Code Playgroud)
但是,由于构成设置名称的部分数量可能会有所不同,例如newSettingName可能是"Modules_Floorplan_Image_Src",我想使用诸如...之类的函数动态执行此操作.
createSetting (newSettingNameArray, newSettingValue);
function createSetting(setting, value) {
// code to create new setting goes here
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决如何动态地这样做吗?
我假设在那里必须有一个for循环才能通过数组进行迭代,但是我还没有找到一种方法来创建嵌套对象.
如果你有这么远,非常感谢你花时间阅读,即使你无法帮助.
我有一个功能,设置如下
function mainFunction() {
function subFunction() {
var str = "foo";
return str;
}
}
var test = mainFunction();
alert(test);
Run Code Online (Sandbox Code Playgroud)
根据我的逻辑,该警报应该返回'foo',而是返回undefined.我究竟做错了什么?
更新:这是我的实际代码(它是使用Google API进行反向地理编码的功能)
function reverseGeocode(latitude,longitude){
var address = "";
var country = "";
var countrycode = "";
var locality = "";
var geocoder = new GClientGeocoder();
var latlng = new GLatLng(latitude, longitude);
return geocoder.getLocations(latlng, function(addresses) {
address = addresses.Placemark[0].address;
country = addresses.Placemark[0].AddressDetails.Country.CountryName;
countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode;
locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
return country;
});
}
Run Code Online (Sandbox Code Playgroud) 我在页面中有一个表格,其中包含左侧单元格中的复选框和右侧单元格中的说明."描述"包含h4标题和纯文本.我想把整个描述(里面的一切<td></td>
)都做成标签.
所以每一行看起来像这样:
<tr><td><input type="checkbox" name="entiries[]" value="i1" id="i1"></td>
<td><label for="i1">
<h4>Some stuff</h4>more stuff..
<h4>Some stuff</h4>more stuff..
</label>
</td></tr>
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,文本不像标签那样且不可点击.我正在使用Firefox 3.6进行测试.如果我删除<h4>
标签它开始工作,但这使格式化变得复杂.<h*>
标签是否存在阻止<label>
正常工作的问题?
我是python的新手,我希望我能用.
符号来访问a的值dict
.
让我们说我test
喜欢这样:
>>> test = dict()
>>> test['name'] = 'value'
>>> print(test['name'])
value
Run Code Online (Sandbox Code Playgroud)
但是,我希望我能做到test.name
让value
.事实上,我通过覆盖__getattr__
我的类中的方法来做到这一点:
class JuspayObject:
def __init__(self,response):
self.__dict__['_response'] = response
def __getattr__(self,key):
try:
return self._response[key]
except KeyError,err:
sys.stderr.write('Sorry no key matches')
Run Code Online (Sandbox Code Playgroud)
这很有效!当我做:
test.name // I get value.
Run Code Online (Sandbox Code Playgroud)
但问题是当我test
单独打印时,我得到的错误是:
'Sorry no key matches'
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我还在学习如何将SAS代码翻译成R,然后收到警告.我需要了解我犯错误的地方.我想要做的是创建一个变量来总结和区分人口的3种状态:大陆,海外,外国人.我有一个包含2个变量的数据库:
idnat
法国人,外国人),如果idnat
是法国人那么:
idbp
大陆,殖民地,海外)我想从汇总信息idnat
,并idbp
进入一个所谓的新变量idnat2
:
所有这些变量都使用"字符类型".
列idnat2中预期的结果:
idnat idbp idnat2
1 french mainland mainland
2 french colony overseas
3 french overseas overseas
4 foreign foreign foreign
Run Code Online (Sandbox Code Playgroud)
这是我要在R中翻译的SAS代码:
if idnat = "french" then do;
if idbp in ("overseas","colony") then idnat2 = "overseas";
else idnat2 = "mainland";
end;
else idnat2 = "foreigner";
run;
Run Code Online (Sandbox Code Playgroud)
这是我在R中的尝试:
if(idnat=="french"){
idnat2 <- "mainland"
} else if(idbp=="overseas"|idbp=="colony"){
idnat2 <- "overseas"
} else {
idnat2 <- …
Run Code Online (Sandbox Code Playgroud) 好的,我注意到了一些东西,但在CSS规范中找不到它.使用样式设置position: fixed
将相对于浏览器视口绝对定位它.如果将固定位置元素放在另一个元素中会发生什么?CSS示例如下:
.fixed {
position: fixed;
width: 100px;
height: 100px;
background: red;
}
#parent { right 100px; padding: 40px; }
.fixed .fixed { background: blue; }
Run Code Online (Sandbox Code Playgroud)
和HTML:
<div id="parent" class="fixed"> <div class="fixed"> </div> </div>
Run Code Online (Sandbox Code Playgroud)
据我所知,该元素相对于其最近的父级也是固定定位的,该父级也是固定定位的.这是否适用于所有浏览器; 还有,这是一个错误,还是故意的行为?
到目前为止,我还没有在互联网上找到关于这个主题的任何内容,只是"固定位置使其坚持页面".
如果我需要创建几个嵌套的DOM元素,我知道一种方法,就是将它们写成长字符串,然后使用合适的jQuery函数将它们放在文档中.就像是:
elem.html(
'<div class="wrapper">
<div class="inner">
<span>Some text<span>
</div>
<div class="inner">
<span>Other text<span>
</div>
</div>');
Run Code Online (Sandbox Code Playgroud)
这种方式显然不是最干净的.刺痛并不需要太长时间来弄乱,编辑就成了问题.我更喜欢这种表示法:
$('<div></div>', {
class : 'inner'
})
.appendTo( elem );
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何在上面动态创建嵌套元素时有效地实现它.因此,如果有第二个符号的第一个例子,我会很高兴了解它.
基本上,问题是,什么是动态创建嵌套HTML元素的最佳方法,而不必处理凌乱的长字符串?
注意:我知道模板引擎.然而,这是一个关于动态创建几个HTML元素的问题.就像为插件或类似案例构建DOM依赖项时一样.
我有一个扁平的字典,我想把它变成一个嵌套的字典
flat = {'X_a_one': 10,
'X_a_two': 20,
'X_b_one': 10,
'X_b_two': 20,
'Y_a_one': 10,
'Y_a_two': 20,
'Y_b_one': 10,
'Y_b_two': 20}
Run Code Online (Sandbox Code Playgroud)
我想将其转换为表单
nested = {'X': {'a': {'one': 10,
'two': 20},
'b': {'one': 10,
'two': 20}},
'Y': {'a': {'one': 10,
'two': 20},
'b': {'one': 10,
'two': 20}}}
Run Code Online (Sandbox Code Playgroud)
扁平字典的结构使得模糊不应存在任何问题.我希望它适用于任意深度的字典,但性能并不是真正的问题.我已经看到很多用于展平嵌套字典的方法,但基本上没有用于嵌套扁平字典的方法.存储在字典中的值是标量或字符串,永远不会迭代.
到目前为止,我有一些可以接受输入的东西
test_dict = {'X_a_one': '10',
'X_b_one': '10',
'X_c_one': '10'}
Run Code Online (Sandbox Code Playgroud)
到输出
test_out = {'X': {'a_one': '10',
'b_one': '10',
'c_one': '10'}}
Run Code Online (Sandbox Code Playgroud)
使用代码
def nest_once(inp_dict):
out = {}
if isinstance(inp_dict, dict):
for key, val in inp_dict.items():
if '_' …
Run Code Online (Sandbox Code Playgroud)