无法安装模块pip3,从2014年12月的最高投票线程尝试了几个建议,但仍然得到以下内容:
sudo pip3 install send2trash
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
load_entry_point('pip==1.5.6', 'console_scripts', 'pip3')()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 558, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2682, in load_entry_point
return ep.load()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2355, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2361, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 74, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/usr/lib/python3/dist-packages/pip/vcs/mercurial.py", line 9, in <module>
from pip.download …Run Code Online (Sandbox Code Playgroud) 为什么在函数中执行一组命令:
def main():
[do stuff]
return something
print(main())
Run Code Online (Sandbox Code Playgroud)
在python中运行速度比在顶层执行命令1.5x要3x快得多:
[do stuff]
print(something)
Run Code Online (Sandbox Code Playgroud) 在python中检查是否至少指定了该函数的一个默认参数是什么?
我们假设我们有一些功能:
def some_function(arg_a=None, arg_b=None, arg_c=False)
Run Code Online (Sandbox Code Playgroud)
有一些默认参数.就我而言,我需要检查,如果任一arg_a或arg_b指定.所以我想实现这样的事情:
def some_function(arg_a=None, arg_b=None, arg_c=False):
...
if arg_a is not None:
...
elif arg_b is not None:
...
else:
raise ValueError('Expected either arg_a or arg_b args')
...
...
Run Code Online (Sandbox Code Playgroud)
那么,实现这种功能的更多pythonic方法是什么?
PEP 484说"使用类型提示进行性能优化是留给读者的练习." 这告诉我,就像Common Lisp一样,当我发誓我知道自己在做什么时,类型声明可用于在性能密集型函数中预留类型调度.为了自己尝试这个,我用一个p系列计算了一个基准来计算pi.首先,我以天真的方式做,然后我尝试聪明并利用类型提示的性能:
import math
import time
def baselpi0(n):
baselsum = 0;
for i in range(1,n):
baselsum += 1.0 / (i * i)
return math.sqrt(6.0 * baselsum)
def baselpi1(n : int) -> float:
n = float(n)
baselsum = 0.0
i = 1.0
while i < n:
baselsum += 1.0 / (i * i)
i += 1.0
return math.sqrt(6.0 * baselsum)
start = time.time()
print(baselpi0(1000000000))
end = time.time()
print(end - start)
start = time.time()
print(baselpi1(1000000000))
end = time.time()
print(end - …Run Code Online (Sandbox Code Playgroud) 假设我有如下代码:
def lab():
letter = prompt()
def experiment_1():
if letter == 1:
print("check")
#Would there be a way to quit the lab function right here?
def experiment_2():
if letter == 1:
print("check2")
experiment_1()
experiment_2()
lab()
Run Code Online (Sandbox Code Playgroud)
打印“支票”后,我是否可以立即退出实验室功能?我尝试将 return 放在最后,experiment_1但这似乎直接转到下一个函数,即experiment_2.
我试图在python中围绕async/await.
我是在正确的轨道上吗?
async和@coroutine函数返回coroutine/generator,而不是返回值.await 提取协程/发生器的实际返回值.
async 函数结果(coroutines)意味着要添加到事件循环中.
await 在事件循环和等待的协程之间创建"桥梁"(启用下一个点).@coroutine的yield直接与事件循环通信.(跳过等待结果的直接来电者)
await 只能在异步函数中使用.
yield只能在里面使用@coroutine.(@coroutine= @types.coroutine)
我试图通过使用模块中的signature()函数,基于我在一些Python函数中提供的类型注释生成一些JavaScript inspect.
当类型是一个简单的内置类时,这部分可以正常工作:
import inspect
def my_function() -> dict:
pass
signature = inspect.signature(my_function)
signature.return_annotation is dict # True
Run Code Online (Sandbox Code Playgroud)
虽然我不知道如何打开和检查更复杂的注释,例如:
from typing import List
import inspect
def my_function() -> List[int]:
pass
signature = inspect.signature(my_function)
signature.return_annotation is List[int] # False
Run Code Online (Sandbox Code Playgroud)
再次引用自定义类的类似问题:
def my_function() -> List['User']:
pass
...
signature.return_annotation # typing.List[_ForwardRef('User')]
Run Code Online (Sandbox Code Playgroud)
我想要的是这样的 - 所以我可以在生成JavaScript时适当地分支:
type = signature.return_annotation... # list
member_type = signature.return_annotation... # int / 'User'
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有一份清单清单:
a = [[1, 3, 4], [2, 5, 7]]
Run Code Online (Sandbox Code Playgroud)
我希望输出格式如下:
1 3 4
2 5 7
Run Code Online (Sandbox Code Playgroud)
我已经尝试过以下方式,但输出不是所需的方式:
for i in a:
for j in i:
print(j, sep=' ')
Run Code Online (Sandbox Code Playgroud)
输出:
1
3
4
2
5
7
Run Code Online (Sandbox Code Playgroud)
在更改打印调用时使用end:
for i in a:
for j in i:
print(j, end = ' ')
Run Code Online (Sandbox Code Playgroud)
输出:
1 3 4 2 5 7
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我在理解为什么解压缩不能与Python 2.7中的list和print语句一起使用时遇到问题:
>>> l=['a', 'b', 'c']
>>> print (*l, sep='')
Run Code Online (Sandbox Code Playgroud)
Python 3.x工作正常并打印:
abc
Run Code Online (Sandbox Code Playgroud)
但是,Python 2.7引发了一个错误:
print (*l, sep='')
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我怎样才能使它适用于Python 2.7?
我知道我可以使用join来代码编写: ''.join(l)
我正在使用 Python3 并且刚刚学会了如何使用mypy. 我正在阅读文档(特别是这部分似乎相关),但找不到我的问题的任何答案:
是否可以为类型定义一些快捷方式?
例子:
而不是写作
from typing import List
def f(x: List[int]) -> List[int]:
return x[1:]
Run Code Online (Sandbox Code Playgroud)
我想拥有
from typing import List
sequence = DefineTypeShortcut(List[int])
def f(x: sequence) -> sequence:
return x[1:]
Run Code Online (Sandbox Code Playgroud)
只是为了澄清,我不想定义一个新类Sequence,我只是想要更容易阅读的函数签名。
python ×10
python-3.x ×9
type-hinting ×3
printing ×2
python-2.7 ×2
annotations ×1
async-await ×1
function ×1
inspect ×1
list ×1
mypy ×1
nested-lists ×1
optimization ×1
pip ×1