我想知道是否有人能想出一种方法来检查一个函数是否需要在Python中返回一个有意义的值.也就是说,检查返回值是否将用于任何事情.我猜测答案是否定的,重构我的程序流程会更好.有问题的函数从网络套接字中提取其返回值.如果不会使用返回值,我不想浪费获取结果的资源.
我已经尝试使用回溯来发现调用行,但这不起作用.这是我想到的一个例子:
>>> def func():
... print should_return()
...
>>> func()
False
>>> ret = func()
True
Run Code Online (Sandbox Code Playgroud)
函数"知道"正在分配其返回值.
这是我目前的解决方法:
>>> def func(**kwargs):
... should_return = kwargs.pop('_wait', False)
... print should_return
...
>>> func()
False
>>> ret = func(_wait=True)
True
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个程序,分别同时读取和写入进程的std(out/in).但是,似乎在线程中写入程序的stdin不起作用.这是相关的代码:
import subprocess, threading, queue
def intoP(proc, que):
while True:
if proc.returncode is not None:
break
text = que.get().encode() + b"\n"
print(repr(text)) # This works
proc.stdin.write(text) # This doesn't.
que = queue.Queue(-1)
proc = subprocess.Popen(["cat"], stdin=subprocess.PIPE)
threading.Thread(target=intoP, args=(proc, que)).start()
que.put("Hello, world!")
Run Code Online (Sandbox Code Playgroud)
出了什么问题,有没有办法解决它?
我在Mac OSX上运行python 3.1.2,它确认它在python2.7中运行.
我在我的节目一个可笑的代码段现在:
str(len(str(len(var_text)**255)))
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来缩短它?因为,坦率地说,这太荒谬了.
转换一个数> 500位到科学记数法一个选项也将是有益的(这就是我想要做的)
完整代码:
print("Useless code rating:" , str(len(var_text)**255)[1] + "e" + str(len(str(len(var_text)**255))))
Run Code Online (Sandbox Code Playgroud) 我有一个拥有大量领域的联盟,一些不确定的大小.我想创建一个预设值(以NULL为单位),其中union的每个字段都为零.我尝试了以下,但没有优化它可能会导致两个副本; 更重要的是,实际上并没有起作用.
标题:
extern union MyUnion my_union_zero;
Run Code Online (Sandbox Code Playgroud)
资源:
static char my_union_zero_arr[sizeof(union MyUnion)] = {0};
union MyUnion my_union_zero = (union MyUnion)my_union_zero_arr;
Run Code Online (Sandbox Code Playgroud)
我错过了一些明显的东西吗 有没有一种标准的方法来实现这一目标?
作为一个大型python程序的一部分,我有以下代码:
for arg in sys.argv:
if "name=" in arg:
name = arg[5:]
print(name)
elif "uname=" in arg:
uname = arg[6:]
print(uname)
elif "password=" in arg:
password = arg[9:]
print(password)
elif "bday=" in arg:
bday = arg[5:]
print(bday)
else:
pass
Run Code Online (Sandbox Code Playgroud)
该计划期望如下:
python prog.py "name=Kevin" "uname=kevin" "password=something" "bday=01/01/01"
Run Code Online (Sandbox Code Playgroud)
当我稍后尝试使用"uname"时,程序失败,声称"uname未定义"我添加了"print()"行来尝试和调试,"print(uname)"总是显示"= kevin"而不管我放在那里的索引号(这里是"6:").其他陈述似乎工作正常.这是python中的错误吗?我很迷茫.
提前致谢.
我正在寻找一种(最好是简单的)方法来查找和排序python流元素中最常见的字节.
例如
>>> freq_bytes(b'hello world')
b'lohe wrd'
Run Code Online (Sandbox Code Playgroud)
甚至
>>> freq_bytes(b'hello world')
[108,111,104,101,32,119,114,100]
Run Code Online (Sandbox Code Playgroud)
我目前有一个函数返回表单中的列表list[97] == occurrences of "a".我需要对它进行排序.
我想我基本上需要翻转列表,以便list[a] = b --> list[b] = a同时删除重复.