Python 2有内置函数execfile,在Python 3.0中删除了.这个问题讨论了Python 3.0的替代方案,但自Python 3.0以来已经做了一些重大的改变.
execfilePython 3.2和未来的Python 3.x版本的最佳替代方案是什么?
我正在尝试使用BEncoding ObjC类来解码.torrent文件.
NSData *rawdata = [NSData dataWithContentsOfFile:@"/path/to/the.torrent"];
NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
Run Code Online (Sandbox Code Playgroud)
当我NSLog torrent得到以下内容时:
{
announce = <68747470 3a2f2f74 6f727265 6e742e75 62756e74 752e636f 6d3a3639 36392f61 6e6e6f75 6e6365>;
comment = <5562756e 74752043 44207265 6c656173 65732e75 62756e74 752e636f 6d>;
"creation date" = 1225365524;
info = {
length = 732766208;
name = <7562756e 74752d38 2e31302d 6465736b 746f702d 69333836 2e69736f>;
"piece length" = 524288;
....
Run Code Online (Sandbox Code Playgroud)
如何将其name转换为NSString?我试过了..
NSData *info = [torrent valueForKey:@"info"];
NSData *name = [info valueForKey:@"name"]; …Run Code Online (Sandbox Code Playgroud) 经常声明在Python 2 中super应该避免使用.我super在Python 2中使用它发现它永远不会按照我的预期行事,除非我提供所有参数,例如:
super(ThisClass, self).some_func(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
在我看来,这违背了使用的目的super(),它既不简洁,也不比它更好TheBaseClass.some_func(self, *args, **kwargs).在大多数情况下,方法解析顺序是一个遥远的童话故事.
super要继续使用?如何从父目录进行相对导入?
来自meme/cmd/meme:
import "../../../meme"
Run Code Online (Sandbox Code Playgroud)
这给出了一个模棱两可的错误:
matt@stanley:~/gopath/src/bitbucket.org/anacrolix/meme/cmd/meme$ go get bitbucket.org/anacrolix/meme/cmd/meme
can't load package: /home/matt/gopath/src/bitbucket.org/anacrolix/meme/cmd/meme/main.go:8:2: local import "../../../meme" in non-local package
matt@stanley:~/gopath/src/bitbucket.org/anacrolix/meme/cmd/meme$ echo $GOPATH
/home/matt/gopath
Run Code Online (Sandbox Code Playgroud)
如何从父目录导入本地?
如何仅使用proc获取所有网络接口的(IPv4)地址?经过一番广泛调查后,我发现了以下内容:
ifconfig利用SIOCGIFADDR,需要打开套接字并提前了解所有接口名称.它也没有记录在Linux上的任何手册页中.proc包含/proc/net/dev,但这是一个接口统计信息列表.proc包含/proc/net/if_inet6,这正是我所需要的,但对于IPv6.proc,但实际地址很少使用,除非明确部分连接.getifaddrs,这是一个非常"神奇"的功能,你期望在Windows中看到它.它也在BSD上实现.然而,它不是非常面向文本的,这使得很难使用非C语言.在下面的代码中引发A第二个(B)时,我的第一个异常()会发生什么?
class A(Exception): pass
class B(Exception): pass
try:
try:
raise A('first')
finally:
raise B('second')
except X as c:
print(c)
Run Code Online (Sandbox Code Playgroud)
如果运行X = A我得到:
Traceback (most recent call last):
File "raising_more_exceptions.py", line 6, in
raise A('first')
__main__.A: first
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "raising_more_exceptions.py", line 8, in
raise B('second')
__main__.B: second
但如果X = B我得到:
second
这个问题专门针对Python 3,因为它的异常处理与Python 2完全不同.
在concurrent.futures.Executor.map采用可变数目从该给出的函数被调用iterables的.如果我有一个生成通常解压缩的元组的生成器,我应该如何调用它?
以下方法不起作用,因为每个生成的元组都作为map的不同参数给出:
args = ((a, b) for (a, b) in c)
for result in executor.map(f, *args):
pass
Run Code Online (Sandbox Code Playgroud)
如果没有生成器,映射的所需参数可能如下所示:
executor.map(
f,
(i[0] for i in args),
(i[1] for i in args),
...,
(i[N] for i in args),
)
Run Code Online (Sandbox Code Playgroud) python ×6
python-3.x ×3
import ×2
linux ×2
bash ×1
bittorrent ×1
build ×1
cocoa ×1
concurrency ×1
datetime ×1
exception ×1
execfile ×1
future ×1
gnu ×1
gnu-make ×1
go ×1
ipv4 ×1
iterator ×1
localtime ×1
makefile ×1
map-function ×1
networking ×1
objective-c ×1
procfs ×1
python-2.x ×1
shell ×1
subshell ×1
super ×1
timezone ×1