小编Mar*_*nen的帖子

为什么'x'中的'x'比'x'=='x'快?

>>> 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到处而不是==!

python performance python-3.x python-internals

273
推荐指数
2
解决办法
2万
查看次数

在PyCharm中保存时自动添加换行符?

PyCharm 5抱怨文件末尾缺少换行符:

PEP 8:文件末尾没有换行符

每当我保存文件时,如何告诉PyCharm自动添加换行符(如果缺少)?

python newline pycharm

44
推荐指数
2
解决办法
1万
查看次数

QTextEdit vs QPlainTextEdit

有什么区别QTextEditQPlainTextEdit,为什么使用一个而不是另一个?

我正在编写一个文本编辑器作为学习Qt5的练习,现在我想知道是否使用QTextEditQPlainTextEdit.到目前为止,我只发现你可以显示图像QTextEdit,但除此之外,它们看起来与我相似.我的文本编辑器应该支持一些基本的语法高亮(可能使用textChanged()信号),但这几乎与需求一样.

谷歌搜索"QTextEdit vs QPlainTextEdit""QTextEdit与QPlainTextEdit相比"并没有给我任何可以比较两个类的结果.

c++ qt qtextedit qplaintextedit qt5

41
推荐指数
1
解决办法
3万
查看次数

使用List/Tuple/etc.从输入vs直接引用类型为list/tuple/etc.

什么是使用的差异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)

我何时应该使用另一个?

python typing type-hinting python-3.5

30
推荐指数
3
解决办法
8059
查看次数

为什么使用contextlib.suppress而不是try/except with pass?

为什么人会使用contextlib.suppress来抑制异常,而不是try/ exceptpass

这两种方法之间的字符数没有区别(如果有的话,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)

python python-3.x

23
推荐指数
2
解决办法
4099
查看次数

查询参数(axios请求)中具有相同键的多个字段?

所以后端(不在我的控制之下)需要一个像这样的查询字符串:

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)

显然,这样的对象不能有多个具有相同键的字段.

如何使用相同的密钥发送包含多个字段的请求?

javascript parameters request axios

23
推荐指数
3
解决办法
2万
查看次数

打字.任何vs对象?

使用typing.Anyobject打字相比有什么区别吗?例如:

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)

python type-hinting python-3.5

21
推荐指数
2
解决办法
4332
查看次数

避免复制子类中的所有构造函数

所以基类有多个构造函数:

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)

如您所见,我必须为每个子类重复这三个构造函数.有没有办法避免这种情况,因为构造函数通常不做任何特殊的事情?有时我需要一些特定于类的初始化,所以直接复制所有东西并不总是可行的.

c++ inheritance constructor

20
推荐指数
3
解决办法
2834
查看次数

如何在python中将字符串转换为有效的变量名?

我需要将任意字符串转换为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方式来做到这一点?

python string variables validation

18
推荐指数
3
解决办法
8594
查看次数

在函数调用期间向kwargs添加参数?

有没有办法在函数调用期间将键值对添加到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 kwargs keyword-argument python-3.x

17
推荐指数
1
解决办法
2万
查看次数