我有以下代码:
class A(object):
def __init__(self):
self.name = "A"
super(A, self).__init__()
def Update(self):
print "Update A"
self.PickTarget()
def PickTarget(self):
print "PickTarget A"
class B(object):
def __init__(self):
self.name = "B"
super(B, self).__init__()
def Update(self):
print "Update B"
self.PickTarget()
def PickTarget(self):
print "PickTarget B"
class C(A, B):
def __init__(self):
super(C, self).__init__()
def Update(self, useA):
if useA:
A.Update(self)
else:
B.Update(self)
c = C()
c.Update(useA = True)
# prints:
# Update A
# PickTarget A
c.Update(useA = False)
# prints:
# Update B
# PickTarget …Run Code Online (Sandbox Code Playgroud) 我有一个字典,它使用4元组作为它的关键.我需要找到字典中与部分其他元组部分匹配的所有键.我有一些代码可以做到这一点,但它很慢,需要优化.
这就是我追求的:
Keys:
(1, 2, 3, 4)
(1, 3, 5, 2)
(2, 4, 8, 7)
(1, 4, 3, 4)
Match:
(1, None, 3, None)
Result:
[(1, 2, 3, 4), (1, 4, 3, 4)]
Run Code Online (Sandbox Code Playgroud)
当前代码:
def GetTuples(self, keyWords):
tuples = []
for k in self.chain.iterkeys():
match = True
for i in range(self.order):
if keyWords[i] is not None and keyWords[i] != k[i]:
match = False
break
if match is True:
tuples.append(k)
return tuples
Run Code Online (Sandbox Code Playgroud)
我基本上都在寻找对这种方法的优化,或者更好的存储方法.
我正在更新AmMap地图的地图区域,但只要我这样做,就会重置缩放级别和位置.
我更新地图的功能是:
function setData(data) {
var parsedData = JSON.parse(data);
map.dataProvider.areas = parsedData.areas;
map.validateData();
}
Run Code Online (Sandbox Code Playgroud)
我尝试了很多东西,但我无法让它发挥作用.
欢呼任何帮助.
def Test(value):
def innerFunc():
print value
innerFunc()
def TestWithAssignment(value):
def innerFunc():
print value
value = "Changed value"
innerFunc()
Test("Hello 1")
# Prints "Hello 1"
TestWithAssignment("Hello 2")
# Throws UnboundLocalError: local variable 'value' referenced before assignment
# on the "print value" line
Run Code Online (Sandbox Code Playgroud)
为什么第二个失败,因为唯一的区别是在print语句之后的赋值?我对此非常困惑.