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(),那么一切正常.我在这里不正确地调用超级(类型)还是有些东西我不见了?
我知道您可以使用ctrl-w + enter在新的水平窗口中打开quickfix项目.
有没有办法在垂直拆分中从quickfix窗口打开一个项目?
使用空元组作为函数的可迭代参数的默认值是否有任何缺点?假设你在函数中想要的是一个不可变的迭代.例如
def foo(a, b=()):
print a
for x in b:
print x
Run Code Online (Sandbox Code Playgroud)
我似乎找不到这个用例的很多例子.
使用Class()或self .__ class __()在类中创建新对象有哪些优点/缺点?一种方式通常优先于另一种方式吗?
这是我正在谈论的一个人为的例子.
class Foo(object):
def __init__(self, a):
self.a = a
def __add__(self, other):
return Foo(self.a + other.a)
def __str__(self):
return str(self.a)
def add1(self, b):
return self + Foo(b)
def add2(self, b):
return self + self.__class__(b)
Run Code Online (Sandbox Code Playgroud) 使用"除无"之外是否有任何意想不到的副作用?我期望的行为是,一些小的测试似乎证实了该条款不会捕获任何内容.
这是我想要做的大致概述.如果没有为函数提供参数,则exceptions = None会创建"除None"子句.只是想仔细检查一下,我不会发现意想不到的事情.
# exceptions is exception or set of exceptions I want to do special processing for
def check_exceptions(exceptions=None)
try:
...
except exceptions as e:
...
Run Code Online (Sandbox Code Playgroud) http://docs.python.org/2/reference/expressions.html#operator-precedence
我的猜测是它落入了dict查找之上的一个桶中
func(*mydict[mykey])
Run Code Online (Sandbox Code Playgroud)
字典首先查找.有没有比我的初始链接更好的图表,更详细地介绍了python中的操作顺序?
有没有办法在Linux中定期读取性能计数器?
喜欢的东西perf stat
与每一个X周期采样能力就是我要找的.
基本上我希望能够为某些程序每X量的cpu周期读取指令计数器(执行的指令数).
是否有可能恢复使用rsync -avz -delete删除的文件?如果是,那么有什么建议的工具呢?
当我尝试使用curve_fit进行指数拟合时,scipy会返回错误.难道我做错了什么?从np.exp(-b*t)中删除负号可以使curve_fit工作,但它返回的值却是偏离的.
#!/usr/bin/python
import numpy as np
import scipy as sp
from scipy.optimize import curve_fit
import scipy.optimize as opt
import matplotlib.pyplot as plt
x = [40,45,50,55,60]
y = [0.99358851674641158, 0.79779904306220106, 0.60200956937799055, 0.49521531100478472, 0.38842105263157894]
def model_func(t, a, b, c):
return a * np.exp(-b * t) + c
opt_parms, parm_cov = sp.optimize.curve_fit(model_func, x, y, maxfev=1000)
a,b,c = opt_parms
print a,b,c
print x
print y
print model_func(x, a,b,c)
Run Code Online (Sandbox Code Playgroud)
失败错误:
Traceback (most recent call last):
File "asdf.py", line 18, in <module>
opt_parms, parm_cov = sp.optimize.curve_fit(model_func, …
Run Code Online (Sandbox Code Playgroud) 为什么跟随两个代码片段会产生不同的错误?我理解字符串是可迭代的,但是我不明白为什么这在这里很重要,因为集合是被迭代的对象.
s = set([1, 2])
for one, two in s:
print one, two
Run Code Online (Sandbox Code Playgroud)
加薪
Traceback (most recent call last):
File "asdf.py", line 86, in <module>
for one, two in s:
TypeError: 'int' object is not iterable
Run Code Online (Sandbox Code Playgroud)
s2 = set(['a', 'b'])
for one, two in s2:
print one, two
Run Code Online (Sandbox Code Playgroud)
加薪
Traceback (most recent call last):
File "asdf.py", line 90, in <module>
for one, two in s2:
ValueError: need more than 1 value to unpack
Run Code Online (Sandbox Code Playgroud) 我想知道在unicode字符串上调用str()时内部会发生什么.
# coding: utf-8
s2 = str(u'hello')
Run Code Online (Sandbox Code Playgroud)
s2只是str()arg的unicode字节表示吗?
python ×8
linux ×2
python-2.7 ×2
ext4 ×1
matplotlib ×1
perf ×1
rsync ×1
scipy ×1
super ×1
ubuntu-11.10 ×1
unicode ×1
vim ×1