>>> timeit.timeit("'x' in ('x',)")
0.04869917374131205
>>> timeit.timeit("'x' == 'x'")
0.06144205736110564
Run Code Online (Sandbox Code Playgroud)
也适用于具有多个元素的元组,两个版本似乎线性增长:
>>> timeit.timeit("'x' in ('x', 'y')")
0.04866674801541748
>>> timeit.timeit("'x' == 'x' or 'x' == 'y'")
0.06565782838087131
>>> timeit.timeit("'x' in ('y', 'x')")
0.08975995576448526
>>> timeit.timeit("'x' == 'y' or 'x' == 'y'")
0.12992391047427532
Run Code Online (Sandbox Code Playgroud)
基于此,我认为我应该完全开始使用in到处而不是==!
有什么区别QTextEdit和QPlainTextEdit,为什么使用一个而不是另一个?
我正在编写一个文本编辑器作为学习Qt5的练习,现在我想知道是否使用QTextEdit或QPlainTextEdit.到目前为止,我只发现你可以显示图像QTextEdit,但除此之外,它们看起来与我相似.我的文本编辑器应该支持一些基本的语法高亮(可能使用textChanged()信号),但这几乎与需求一样.
谷歌搜索"QTextEdit vs QPlainTextEdit"和"QTextEdit与QPlainTextEdit相比"并没有给我任何可以比较两个类的结果.
什么是使用的差异List,Tuple等等.从typing模块:
from typing import Tuple
def f(points: Tuple):
return map(do_stuff, points)
Run Code Online (Sandbox Code Playgroud)
而不是直接引用Python的类型:
def f(points: tuple):
return map(do_stuff, points)
Run Code Online (Sandbox Code Playgroud)
我何时应该使用另一个?
为什么人会使用contextlib.suppress来抑制异常,而不是try/ except用pass?
这两种方法之间的字符数没有区别(如果有的话,suppress有更多的字符),即使代码通常用LOC而不是字符计算,在两种情况下,当出现错误时,suppress似乎也比try/ 慢得多except被提出,当它不是:
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from timeit import timeit
>>> # With an error
>>> timeit("""with suppress(ValueError):
x = int('a')""", setup="from contextlib import suppress")
1.9571568971892543
>>> timeit("""try:
x = int('a')
except ValueError:
pass""")
1.0758466499161656
>>> # With no error
>>> timeit("""with suppress(ValueError):
x …Run Code Online (Sandbox Code Playgroud) 所以后端(不在我的控制之下)需要一个像这样的查询字符串:
http://example.com/?foo=5&foo=2&foo=11
Run Code Online (Sandbox Code Playgroud)
但是axios使用JS对象发送请求参数:
axios.get('http://example.com/', { foo: 5 });
Run Code Online (Sandbox Code Playgroud)
显然,这样的对象不能有多个具有相同键的字段.
如何使用相同的密钥发送包含多个字段的请求?
使用typing.Any与object打字相比有什么区别吗?例如:
def get_item(L: list, i: int) -> typing.Any:
return L[i]
Run Code Online (Sandbox Code Playgroud)
相比:
def get_item(L: list, i: int) -> object:
return L[i]
Run Code Online (Sandbox Code Playgroud) 所以基类有多个构造函数:
sf::Sprite()
sf::Sprite(const Texture& texture)
sf::Sprite(const Texture& texture, const IntRect& rectangle)
Run Code Online (Sandbox Code Playgroud)
我多次将这个类子类化:
class Sub : public sf::Sprite {
public:
Sub() : sf::Sprite() {};
Sub(const Texture& texture) : sf::Sprite(texture) {};
Sub(const Texture& texture, const IntRect& rectangle) : sf::Sprite(texture, rectangle) {};
// My subclass specific code
};
Run Code Online (Sandbox Code Playgroud)
如您所见,我必须为每个子类重复这三个构造函数.有没有办法避免这种情况,因为构造函数通常不做任何特殊的事情?有时我需要一些特定于类的初始化,所以直接复制所有东西并不总是可行的.
我需要将任意字符串转换为python中有效变量名称的字符串.
这是一个非常基本的例子:
s1 = 'name/with/slashes'
s2 = 'name '
def clean(s):
s = s.replace('/','')
s = s.strip()
return s
print clean(s1)+'_'#the _ is there so I can see the end of the string
Run Code Online (Sandbox Code Playgroud)
这是一种非常天真的方法.我需要检查字符串是否包含无效的变量名字符并将其替换为''
什么是pythonic方式来做到这一点?
有没有办法在函数调用期间将键值对添加到kwargs中?
def f(**kwargs):
print(kwargs)
# ...
pre_defined_kwargs = {'a': 1, 'b': 2}
f(**pre_defined_kwargs, c=3)
Run Code Online (Sandbox Code Playgroud)
甚至改变现有的论点?
f(**pre_defined_kwargs, b=3) # replaces the earlier b=2
Run Code Online (Sandbox Code Playgroud)
这两个例子不起作用,因为它们会引起错误
>>> f(**pre_defined_kwargs, c=3)
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
指着论点之间的逗号
python ×7
python-3.x ×3
c++ ×2
python-3.5 ×2
type-hinting ×2
axios ×1
constructor ×1
inheritance ×1
javascript ×1
kwargs ×1
newline ×1
parameters ×1
performance ×1
pycharm ×1
qt ×1
qt5 ×1
qtextedit ×1
request ×1
string ×1
typing ×1
validation ×1
variables ×1