有什么区别:
class Child(SomeBaseClass):
def __init__(self):
super(Child, self).__init__()
Run Code Online (Sandbox Code Playgroud)
和:
class Child(SomeBaseClass):
def __init__(self):
SomeBaseClass.__init__(self)
Run Code Online (Sandbox Code Playgroud)
我已经看到super在只有单继承的类中使用了很多.我可以看到为什么你在多重继承中使用它,但不清楚在这种情况下使用它的优点是什么.
内置函数的slice用途是什么?如何使用它?
我知道Pythonic切片的直接方式 - l1[start:stop:step].我想知道我是否有切片对象,那我该如何使用呢?
我想像配置文件一样使用.py文件.因此,使用{...}符号我可以使用字符串作为键创建字典,但定义顺序在标准python字典中丢失.
我的问题:是否有可能覆盖{...}符号,以便我得到一个OrderedDict()而不是dict()?
我希望用OrderedDict(dict = OrderedDict)简单地覆盖dict构造函数会起作用,但事实并非如此.
例如:
dict = OrderedDict
dictname = {
'B key': 'value1',
'A key': 'value2',
'C key': 'value3'
}
print dictname.items()
Run Code Online (Sandbox Code Playgroud)
输出:
[('B key', 'value1'), ('A key', 'value2'), ('C key', 'value3')]
Run Code Online (Sandbox Code Playgroud) 一个纯粹出于好奇心的问题.这显然是无效的语法:
foo = {}
foo['bar': 'baz']
Run Code Online (Sandbox Code Playgroud)
很明显发生了什么,开发人员从字典定义中移出一行,但没有将它从文字字典声明更改为赋值语法(并且已经适当地模拟了结果).
但我的问题是,为什么Python会TypeError: unhashable type在这里而不是SyntaxError?它试图散列的是什么类型?这样做:
'bar': 'baz'
Run Code Online (Sandbox Code Playgroud)
是一个SyntaxError,如下所示:
['bar': 'baz']
Run Code Online (Sandbox Code Playgroud)
所以我看不出什么类型的东西是不可用的.
我做了如下课程:
class MyList(list):
def __init__(self, lst):
self.list = lst
Run Code Online (Sandbox Code Playgroud)
我希望在MyList中覆盖切片功能
我是Python的新手,也是OOP的新手.我有一个错误"...instance has no attribute '__getitem__'",我知道我创建的对象不是列表.我怎样才能成为列表对象.这是类文件:
#!/usr/bin/python -tt
import math, sys, matrix, os
class Point:
'Class for points'
pointCount = 0
def __init__(self, x, y, z):
'initialise the Point from three coordinates'
self.x = x
self.y = y
self.z = z
Point.pointCount += 1
def __str__(self):
'print the Point'
return 'Point (%f, %f, %f)' %(self.x, self.y, self.z)
def copyPoint(self, distance):
'create another Point at distance from the self Point'
return Point(self.x + distance[0], self.y + distance[1], self.z + distance[2]) …Run Code Online (Sandbox Code Playgroud) 如果我有一个数组a,我理解如何以各种方式切片.具体来说,要从任意的第一个索引切片到数组的末尾,我会这样做a[2:].
但是我如何创建切片对象来实现同样的目的呢?这两种方式创建的切片对象的记录是slice(start, stop, step)和slice(stop).
所以,如果我传递一个参数就像我会a[2:]的slice对象将它解释为停止索引,而不是开始索引.
问题:如何slice使用起始索引将索引传递给对象并获取切片对象一直切到最后?我不知道列表的总大小.
我编写了一个数据容器类,它基本上包含一个numpy ndarray成员以及生成time_series掩码/横截面掩码的方法,在ring-buffer模式下获取日期索引(row#),处理调整大小时记住数据可能是一个环形缓冲区,并对形状/尺寸等实施限制.
由于我的类实现,现在我将通过显式引用*.data成员来访问此对象包装的数据.这很麻烦,我想在我的类中实现[]运算符,以便在我的类的实例上调用时,它引用底层ndarray对象上的相同操作.我怎样才能做到这一点?
def MyArray(object):
def __init__(self, shape, fill_value, dtype):
self.shape = shape
self.fill_value = fill_value
self.dtype = dtype
self.data = numpy.empty(shape, fill_value=fill_value, dtype=dtype)
def reset(self, fill_value=None):
self.data.fill(fill_value or self.fill_value)
def resize(self, shape):
if self.data.ndim != len(shape): raise Exception("dim error")
if self.data.shape < shape: raise Exception("sizing down not permitted")
# do resizing
Run Code Online (Sandbox Code Playgroud)
现在,如果我想在其他地方使用这个容器,我必须这样使用它:
arr = MyArray(shape=(10000,20), fill_value=numpy.nan, dtype='float')
arr.data[::10] = numpy.NAN
msk = numpy.random.randn(10000,20)<.5
arr.data[~msk] = -1.
Run Code Online (Sandbox Code Playgroud)
我每次使用它时都需要明确引用arr.data的事实太麻烦且容易出错(我忘记了很多地方的.data后缀).
有没有什么方法可以添加一些运算符,使得切片和索引arr实际上是arr.data 隐式操作的?
python ×8
slice ×3
list ×2
overriding ×2
built-in ×1
dictionary ×1
inheritance ×1
numpy ×1
object ×1
oop ×1
python-2.7 ×1
python-3.x ×1
super ×1