我想要有这样的代码
class myClass():
def __init__(self):
self.__var1 = 'var1'
var2 = 'var2'
def myNormalMethod(self):
print self.__var1
@classmethod
def myClassMethod(cls):
print cls.__var2
#How do I do this?
def myMethod():
print self.__var1
print cls.__var2
Run Code Online (Sandbox Code Playgroud)
现在最后一种方法不起作用,因为我不知道如何同时访问 self 和 cls。我该如何实施?
在我的 vimrc 文件中,我试图编写一个函数,该函数接受两行数字作为参数,然后注释掉该范围内的所有行。我正在使用替代行 '^' 的开头并将其替换为 '#',这在我从函数外部调用时可以正常工作。到目前为止我有这个功能:
function Comment(line1, line2)
a:line1,a:line2s/^/#/g
endfunction
但这不起作用,当我尝试启动 Vim 时出现错误,提示“缺少 :endfunction”
我想要做的是创建一个以字符串流作为参数的方法,在方法中写入它,然后从方法外部的字符串流中读取。
例如这样的事情。
void writeToStream(std::stringstream sstr){
sstr << "Hello World";
}
int main(){
std::stringstream sstr;
writeToStream(sstr);
std::cout << sstr.str();
}
Run Code Online (Sandbox Code Playgroud)
但当我这样做时,我遇到了编译时错误。这可能是因为我应该使用对 stringstream 或类似内容的引用。我尝试过使用引用或指针的不同组合,但似乎没有任何效果?
在编写JavaScript时,通常认为哪些更好.Foo,bar和baz特定于此功能,因此不会在其他功能的任何其他地方使用.
function() {
foo();
bar();
baz();
}
function foo() {
//lines of code
}
function bar() {
//lines of code
}
function baz() {
//lines of code
}
Run Code Online (Sandbox Code Playgroud)
要么
function() {
function foo() {
//lines of code
}
foo();
function bar() {
//lines of code
}
bar();
function baz() {
//lines of code
}
baz();
}
Run Code Online (Sandbox Code Playgroud)
第一个的优点是它更有效,因为如果你更多地调用函数,一旦其他函数只需要创建一次.它也更容易阅读,因为主要外部功能现在只有3行长而且不长.
我能想到的第二个唯一优势是它将这些函数保持为外部函数的私有,并且文件的其余部分无法访问,因为这些函数不会在其他任何地方使用.显然,通常在不需要的时候将事物公之于众,但在像这样的JavaScript情况下则不那么重要.
是否有可能根据调用函数的频率或其他内容选择不同的方式?
我正在尝试为另一个类创建一个Proxy类.我希望这个类在其构造函数中传递给代理,然后让代理动态地创建此类的所有相同方法.
这是我到目前为止无法正常工作的原因:
import inspect
from optparse import OptionParser
class MyClass:
def func1(self):
print 'MyClass.func1'
def func2(self):
print 'MyClass.func1'
class ProxyClass:
def __init__(self):
myClass = MyClass()
members = inspect.getmembers(MyClass, predicate=inspect.ismethod)
for member in members:
funcName = member[0]
def fn(self):
print 'ProxyClass.' + funcName
return myClass[funcName]()
self.__dict__[funcName] = fn
proxyClass = ProxyClass()
proxyClass.func1()
proxyClass.func2()
Run Code Online (Sandbox Code Playgroud)
我认为这是self.__dict__[funcName] = fn需要改变的线,但我不知道该怎么做?
我是Python的新手,所以如果有一种完全不同的Pythonic方式,我也很乐意听到这个.
由于集合只能具有唯一值,这是否意味着每次向元素添加元素时都必须检查它是否等于那里的每个元素,因此是O(n)?
因为如果是这种情况,这会使它们比arrayLists慢很多,那么你应该真正使用它们的唯一时间是确保你的元素都是唯一的还是它们还有其它优点?
我正在看这个问题,答案是有道理的
但是,如果你只想简单地在函数中添加一条日志消息或者简单的东西有什么方法可以避免创建像这样的新承诺
function getStuffDone(param) {
return new Promise(function(resolve, reject) {
// using a promise constructor
myPromiseFn(param+1)
.then(function(val) {
console.log("getStuffDone executing");
resolve(val);
}).catch(function(err) {
console.log("getStuffDone error");
reject(err);
});
});
}
Run Code Online (Sandbox Code Playgroud)
如果你想在这个位置运行promise之前想要一条日志消息怎么办?这会让事情变得更加困难吗?
function getStuffDone(param) {
return new Promise(function(resolve, reject) {
// using a promise constructor
console.log("getStuffDone starting");
myPromiseFn(param+1)
.then(function(val) {
console.log("getStuffDone executing");
resolve(val);
}).catch(function(err) {
console.log("getStuffDone error");
reject(err);
});
});
}
Run Code Online (Sandbox Code Playgroud) 在angular中,这是使用$ http的简单示例.
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("welcome.htm")
.then(function(response) {
$scope.myWelcome = response.data;
});
});
Run Code Online (Sandbox Code Playgroud)
我不明白的是$ http是回调函数中参数的名称,那么为什么如果将它重命名为其他东西会有所不同呢?
例如在JavaScript中,这两个显然是完全相同的功能
function(a, b) {
return a+b;
}
function(c, d) {
return c+d;
}
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,如果我用$ HTTP替换$ http,它会破坏吗?
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $HTTP) {
$HTTP.get("welcome.htm")
.then(function(response) {
$scope.myWelcome = response.data;
});
});
Run Code Online (Sandbox Code Playgroud) 使用 Git 我不明白如何使用 SHA 生成一个 40 位十六进制数字代码,然后可以将其映射到任何可能长达数百行的文件。
我的想法是,假设字符串 '1' -> 00...01,字符串 '2' -> 00..02,字符串 'a34ed..fc' -> a34ed..fc等等,因此哈希映射正在返回自身,然后很明显所有哈希代码都会很快用完,并且任何长度为 41 个字符的字符串都将重用其中一个代码。
我也知道 SHA 并不能保证它永远是唯一的,但我不知道它是如何接近有用的。