当我创建一个包含反斜杠的字符串时,它们会重复:
>>> my_string = "why\does\it\happen?"
>>> my_string
'why\\does\\it\\happen?'
Run Code Online (Sandbox Code Playgroud)
为什么?
我刚刚尝试用Pylint lint一些代码,最后剩下的错误是
R0902: too-many-instance-attributes (8/7)
Run Code Online (Sandbox Code Playgroud)
我理解限制实例属性数量的基本原理,但有七个看起来有点低.我也意识到短号不应该有最后一个字.但是,我想知道我应该做什么,而不是:
def __init__(self, output_file=None, output_dir=None):
"""
Set the frobnicator up, along with default geometries
"""
self.margin = 30
self.pos = [0, 0]
self.sep = [5, 5]
self.cell = [20, 20]
self.frobbr = library.Frobbr()
page = self.frobbr.get_settings('page')
self.lim = [page.get_width() - self.margin,
page.get_height() - self.margin]
self.filename = output_file
self.moddir = output_dir
Run Code Online (Sandbox Code Playgroud)
我应该将几何包装到一个字典中,做一些其他的事情来阻止Pylint抱怨,或者只是忽略它(我真的不想这样做)?
python instance-variables pylint code-readability code-structure
我正在解决项目欧拉的问题,如果我对问题的理解是正确的,我不太确定.
问题8如下:
找到1000位数字中连续五位数的最佳乘积.
我这意味着以下内容:
我需要找到连续运行在1000位数字中的任意五个数字,然后将它们相加以获得总数.我假设数字的大小可以是任何数字,即1,2,3或12,13,14或123,124,124或1234,1235,1236等.
我对此的理解是正确的,还是我误解了这个问题?
注意:请不要提供我需要自己解决的代码或解决方案.
我对Project Euler问题5有一些不同的解决方案,但是这个特定实现中两种语言/平台之间的执行时间差异引起了我的兴趣.我没有使用编译器标志进行任何优化,只是简单javac(通过命令行)和csc(通过Visual Studio).
这是Java代码.它在55ms结束.
public class Problem005b
{
public static void main(String[] args)
{
long begin = System.currentTimeMillis();
int i = 20;
while (true)
{
if (
(i % 19 == 0) &&
(i % 18 == 0) &&
(i % 17 == 0) &&
(i % 16 == 0) &&
(i % 15 == 0) &&
(i % 14 == 0) &&
(i % 13 == 0) &&
(i % 12 == 0) … 从列表中提取项目并将它们作为参数传递给函数调用的简单方法是什么,如下例所示?
例:
def add(a,b,c,d,e):
print(a,b,c,d,e)
x=(1,2,3,4,5)
add(magic_function(x))
Run Code Online (Sandbox Code Playgroud) super()是不是要用于staticmethods?
当我尝试类似的东西
class First(object):
@staticmethod
def getlist():
return ['first']
class Second(First):
@staticmethod
def getlist():
l = super(Second).getlist()
l.append('second')
return l
a = Second.getlist()
print a
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Traceback (most recent call last):
File "asdf.py", line 13, in <module>
a = Second.getlist()
File "asdf.py", line 9, in getlist
l = super(Second).getlist()
AttributeError: 'super' object has no attribute 'getlist'
Run Code Online (Sandbox Code Playgroud)
如果我将staticmethods更改为classmethods并将类实例传递给super(),那么一切正常.我在这里不正确地调用超级(类型)还是有些东西我不见了?
如果我有一个像这样的元组,(1,2,3,4)并且我想将1和3分配给变量a和b,我可以明显地说
myTuple = (1,2,3)
a = my_tuple[0]
b = myTuple[2]
Run Code Online (Sandbox Code Playgroud)
或类似的东西
(a,_,b,_) = myTuple
Run Code Online (Sandbox Code Playgroud)
有没有办法可以解压缩值,但忽略它们中的一个或多个?
Python 3.4 增加了使用静态方法定义函数重载的功能.这基本上是文档中的示例:
from functools import singledispatch
class TestClass(object):
@singledispatch
def test_method(arg, verbose=False):
if verbose:
print("Let me just say,", end=" ")
print(arg)
@test_method.register(int)
def _(arg):
print("Strength in numbers, eh?", end=" ")
print(arg)
@test_method.register(list)
def _(arg):
print("Enumerate this:")
for i, elem in enumerate(arg):
print(i, elem)
if __name__ == '__main__':
TestClass.test_method(55555)
TestClass.test_method([33, 22, 11])
Run Code Online (Sandbox Code Playgroud)
在其最纯粹的形式中,singledispatch实现依赖于第一个用于标识类型的参数,因此将此功能扩展到实例方法变得棘手.
有没有人有任何关于如何使用(或jerry-rig)此功能以使其与实例方法一起使用的建议?
python single-dispatch instance-method python-3.x python-3.4
我将mac更新为Mountain Lion(10.8),现在我用Python和Google App Engine开发的项目不起作用.
GAE库被发现,而标准的Python库(这些是Python库?)被错过(例如cgi,logging,json).
当我打开eclipse(有PyDeV)时,我会收到此警报:
abort: couldn't find mercurial libraries in
[/Library/Python/2.7/site-packages/ /usr/local/bin
/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages] (check your install and
PYTHONPATH). Command line: hg -y debuginstall
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这一问题?
我期待以下两个元组
>>> x = tuple(set([1, "a", "b", "c", "z", "f"]))
>>> y = tuple(set(["a", "b", "c", "z", "f", 1]))
Run Code Online (Sandbox Code Playgroud)
比较不平等,但他们不:
>>> x == y
>>> True
Run Code Online (Sandbox Code Playgroud)
这是为什么?
python ×8
tuples ×2
backslash ×1
c# ×1
comparison ×1
escaping ×1
hashtable ×1
java ×1
list ×1
macos ×1
pylint ×1
python-2.7 ×1
python-3.4 ×1
python-3.x ×1
repr ×1
set ×1
string ×1
super ×1