我正在尝试编写一个python类,它使用需要实例状态信息的装饰器函数.这是按预期工作,但如果我明确地使装饰器成为静态调试,我会收到以下错误:
Traceback (most recent call last):
File "tford.py", line 1, in <module>
class TFord(object):
File "tford.py", line 14, in TFord
@ensure_black
TypeError: 'staticmethod' object is not callable
Run Code Online (Sandbox Code Playgroud)
为什么?
这是代码:
class TFord(object):
def __init__(self, color):
self.color = color
@staticmethod
def ensure_black(func):
def _aux(self, *args, **kwargs):
if self.color == 'black':
return func(*args, **kwargs)
else:
return None
return _aux
@ensure_black
def get():
return 'Here is your shiny new T-Ford'
if __name__ == '__main__':
ford_red = TFord('red')
ford_black = TFord('black')
print ford_red.get()
print ford_black.get() …Run Code Online (Sandbox Code Playgroud) 我想在节点中创建一个子进程,并在光纤中阻塞,直到进程终止.他们我理解它的方式,看起来应该是这样的:
var child_process = require ("child_process");
var Fiber = require ("fibers");
var Future = require ("fibers/future");
var ls = Fiber (function () {
var lsproc = child_process.spawn ("ls");
var lsonSync = Future.wrap (lsproc.on);
console.log ("return: " + lsonSync ("exit").wait ());
}).run ();
Run Code Online (Sandbox Code Playgroud)
节点的响应是:
TypeError: Object #<Object> has no method 'emit'
Run Code Online (Sandbox Code Playgroud)
我认为这与我正在包装实例方法而不是函数这一事实有关,但我不确定如何继续.