以这种方式清空列表似乎很"脏":
while len(alist) > 0 : alist.pop()
Run Code Online (Sandbox Code Playgroud)
这样做有明确的方法吗?
这个示例代码有效(我可以在文件中写一些内容):
from multiprocessing import Process, Queue
queue = Queue()
def _printer(self, queue):
queue.put("hello world!!")
def _cmdDisp(self, queue):
f = file("Cmd.log", "w")
print >> f, queue.get()
f.close()
Run Code Online (Sandbox Code Playgroud)
而不是这个其他样本:( errormsg:'module'对象不可调用)
import Queue
queue = Queue()
def _printer(self, queue):
queue.put("hello world!!")
def _cmdDisp(self, queue):
f = file("Cmd.log", "w")
print >> f, queue.get()
f.close()
Run Code Online (Sandbox Code Playgroud)
这个其他样本没有(我不能在文件中写一些东西):
import Queue
queue = Queue.Queue()
def _printer(self, queue):
queue.put("hello world!!")
def _cmdDisp(self, queue):
f = file("Cmd.log", "w")
print >> f, queue.get()
f.close()
Run Code Online (Sandbox Code Playgroud)
有人可以解释这些差异吗?和权利?
我正在寻找一种方法来处理包含空格的参数,这些空格必须由shell getopts命令解析.
while getopts ":a:i:o:e:v:u:" arg
do
echo "ARG is: $arg" >> /tmp/submit.log
case "$arg" in
a) arg1="$OPTARG" ;;
i) arg2="$OPTARG" ;;
o) arg3="$OPTARG" ;;
...
u) argn="$OPTARG" ;;
-) break ;;
\?) ;;
*) echo "unhandled option $arg" >> /tmp/submit.log ;;
?) echo $usage_string
exit 1 ;;
esac
done
Run Code Online (Sandbox Code Playgroud)
现在,如果-u具有像"STRING WITH WHITE SPACE"这样的参数,而不仅仅是触发了字符串的第一部分,而while循环不会结束.
非常感谢.
我想截取字符串开头 \*#\*
后跟0到7之间的数字
并以:结尾 ##
所以像 \*#\*0##
但是我找不到这个的正则表达式
我以这种方式实现复合模式:
1)“抽象”部分是:
class Component(object):
"""Basic Component Abstraction"""
def __init__(self, *args, **kw):
raise NotImplementedError("must be subclassed")
def status(self):
"""Base Abstract method"""
raise NotImplementedError("must be implemented")
Run Code Online (Sandbox Code Playgroud)
2)一片叶子:
class Leaf(Component):
"""Basic atomic component
"""
def __init__(self, *args, **kw):
self.dict = {}
def status(self):
"""Retrieves properties
"""
return self.dict
Run Code Online (Sandbox Code Playgroud)
问题是 pylint 当然会生成以下警告:
Leaf.__init__: __init__ method from base class 'Component' is not called
Run Code Online (Sandbox Code Playgroud)
但在我的叶子中我不能要求:
def __init__(self, *args, **kw):
Component.__init__(self, *args, **kw)
self.dict = {}
Run Code Online (Sandbox Code Playgroud)
没有引发异常。
我必须忽略 pylint 警告还是存在一些错误的编码?