如果Python无法导入模块virtualenvwrapper.hook_loader,我收到此消息
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 and that PATH is
set properly.
Run Code Online (Sandbox Code Playgroud)
如何在Debian 9中开始设置它?
正如标题所述,我以困惑为例:
class Point(object):
def __init__(self, x=0.0, y=0.0):
self.x, self.y = x, y
@classmethod
def get_point1(cls, cor): # cor is list with x=1 and y=2
return Point(cor[0], cor[1])
@classmethod
def get_point2(cls, cor):
return cls(cor[0], cor[1])
Run Code Online (Sandbox Code Playgroud)
我很困惑我应该使用哪个(get_point1或get_point2),它们之间有什么区别?
对于作业,我必须从以"W"和"Z"开头并以"n"和"t"结尾的文本中打印行(所以Wn,Wt,Zn,Zt组合).我现在有一个代码可行,但似乎有点长,我想知道是否有办法缩短它?
这是代码:
import sys
def main():
for line in sys.stdin:
line = line.rstrip()
if line.startswith("W") and line.endswith("n"):
print(line)
if line.startswith("W") and line.endswith("t"):
print(line)
if line.startswith("Z") and line.endswith("n"):
print(line)
if line.startswith("Z") and line.endswith("t"):
print(line)
main()
Run Code Online (Sandbox Code Playgroud)
正如我所说,它有效,但似乎有点复杂.有关如何缩短的任何提示?
我试过line.startswith("Z","W"),line.endswith("t","n")但我得到一个类型错误(所有切片索引必须是整数或无或有一个__index__method).
我正在Golang网站上巡视,我正在尝试消化其中一个例子.目前还不清楚它是如何工作的:
package main
import "fmt"
func main() {
s := []int{2, 3, 5, 7, 11, 13}
printSlice(s)
// Slice the slice to give it zero length.
s = s[:0]
printSlice(s)
// Extend its length.
s = s[:4]
printSlice(s)
// Drop its first two values.
s = s[2:]
printSlice(s)
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
Run Code Online (Sandbox Code Playgroud)
输出是:
len=6 cap=6 [2 3 5 7 11 13]
len=0 cap=6 []
len=4 cap=6 [2 3 5 7]
len=2 cap=4 …Run Code Online (Sandbox Code Playgroud) 我正在做具有斐波那契功能的memoized装饰练习.当输入变大时,memoized函数应该快得多,因为它从字典返回结果而不是再次计算结果.
我正在使用timeit.timeit()memoized on vs off来测量函数的执行时间.我得到的结果与我的期望完全相反.没有装饰器的执行运行得更快.
# memorized decorator for fibonacci series
def mem_fib(f):
def wrapper(n):
wrapper.d = {} # create the attr member of THIS wrapper
if n in wrapper.d:
return wrapper.d[n]
wrapper.d[n] = f(n) # save f() return in a dict
return wrapper.d[n]
return wrapper
@mem_fib
def fibonacci(n):
assert n >= 0
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
Run Code Online (Sandbox Code Playgroud)
我在PyCharm python的控制台上运行命令.
@with装饰者
>>> print(timeit.timeit('decorators.fibonacci(7)', setup='import decorators'))
19.6940833939
>>> print(timeit.timeit('decorators.fibonacci(10)', setup='import decorators'))
85.7157191166
Run Code Online (Sandbox Code Playgroud)
没有装饰者
>>> …Run Code Online (Sandbox Code Playgroud) 对于TCP套接字,许多客户端可以连接它并发送数据并关闭。但是对于UNIX套接字,如果客户端关闭套接字,则服务器端也会关闭。是吗?
但我认为TCP也具有STOP状态。
这是我的python UNIX套接字代码:
sock_file = "%s_%d.sock" % (sock_name, port)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(sock_file)
sock.listen(1)
Run Code Online (Sandbox Code Playgroud) 我想学习zip类的功能.我写了这个非常简单的例子.
>>> names = ['name1','name2','name3']
>>> ages = ['age1','age2','age3']
>>> print(zip(names, ages))
<zip object at 0x03DB18F0>
>>> zipped = zip(names, ages)
for i in zipped:
type(i)
print(i)
Run Code Online (Sandbox Code Playgroud)
和输出(如预期的那样) -
<class 'tuple'>
('name1', 'age1')
<class 'tuple'>
('name2', 'age2')
<class 'tuple'>
('name3', 'age3')
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样写,请立即在此行后:
for i in zipped:
print(i)
Run Code Online (Sandbox Code Playgroud)
它编译但没有打印任何东西!
要重新检查,我再次这样做了 -
>>> zipped = zip(names, ages)
>>> for i in zipped:
print(i)
('name1', 'age1')
('name2', 'age2')
('name3', 'age3')
Run Code Online (Sandbox Code Playgroud)
这次打印正确.但是在做解压缩时 -
>>> names2, ages2 = zip(*zipped)
Traceback (most recent call last):
File …Run Code Online (Sandbox Code Playgroud) python ×7
debian ×1
decorator ×1
ends-with ×1
go ×1
iterator ×1
python-3.x ×1
slice ×1
sockets ×1
startswith ×1
tcp ×1
unix ×1
virtualenv ×1