小编Emm*_*mma的帖子

如何从列表列表中制作一个平面列表?

我想知道是否有一条快捷方式可以在Python列表中列出一个简单的列表.

我可以在for循环中做到这一点,但也许有一些很酷的"单行"?我用reduce尝试了,但是我收到了一个错误.

l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
reduce(lambda x, y: x.extend(y), l)
Run Code Online (Sandbox Code Playgroud)

错误信息

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
AttributeError: 'NoneType' object has no attribute 'extend'
Run Code Online (Sandbox Code Playgroud)

python list flatten multidimensional-array

2950
推荐指数
29
解决办法
184万
查看次数

覆盖python中的静态方法

在这里参考关于python的绑定和未绑定方法的第一个答案,我有一个问题:

class Test:
    def method_one(self):
        print "Called method_one"
    @staticmethod
    def method_two():
        print "Called method_two"
    @staticmethod
    def method_three():
        Test.method_two()
class T2(Test):
    @staticmethod
    def method_two():
        print "T2"
a_test = Test()
a_test.method_one()
a_test.method_two()
a_test.method_three()
b_test = T2()
b_test.method_three()
Run Code Online (Sandbox Code Playgroud)

产生输出:

Called method_one
Called method_two
Called method_two
Called method_two
Run Code Online (Sandbox Code Playgroud)

有没有办法在python中覆盖静态方法?

我希望b_test.method_three()打印"T2",但它没有(打印"Called method_two"代替).

python static overriding

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

在python中使变量不可继承

有没有办法在python中使变量不可继承?如下例所示:B是A的子类,但我希望它具有自己的SIZE值.

如果B不覆盖SIZE,是否可以获得引发错误(在__init__或getsize()上)?

class A:
   SIZE = 5
   def getsize(self): return self.SIZE

class B(A): pass
Run Code Online (Sandbox Code Playgroud)

编辑:...继承getsize()方法...?

python inheritance

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