小编Fin*_*inn的帖子

停止Sublime Text执行无限循环

当我做的事情

while True:
    print('loop')
Run Code Online (Sandbox Code Playgroud)

并在sublime中执行该代码我无法阻止它.我必须手动终止进程并重新启动sublime.

有没有办法设置某种'max_execution_time'或任何其他解决方法,使我们能够很好地阻止这种情况?

python sublimetext3

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

如何用枚举解压缩元组列表?

我偶然发现了一个我无法解释的解包问题.

这有效:

tuples = [('Jhon', 1), ('Jane', 2)]

for name, score in tuples:
    ...
Run Code Online (Sandbox Code Playgroud)

这也有效

for id, entry in enumerate(tuples):
    name, score = entry
    ...
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

for id, name, score in enumerate(tuples):
    ...
Run Code Online (Sandbox Code Playgroud)

抛出一个ValueError: need more than 2 values to unpack例子.

python tuples python-3.x iterable-unpacking

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

功能与方法的范围

我想知道为什么如果没有定义名称,类的方法不会查看其封闭范围.

def test_scope_function():
    var = 5
    def print_var():
        print(var) # finds var from __test_scope_function__
    print_var()


globalvar = 5
class TestScopeGlobal:
    var = globalvar # finds globalvar from __main__

    @staticmethod
    def print_var():
        print(TestScopeGlobal.var)


class TestScopeClass():
    var = 5

    @staticmethod
    def print_var():
        print(var) # Not finding var, raises NameError

test_scope_function()
TestScopeGlobal.print_var()
TestScopeClass.print_var()
Run Code Online (Sandbox Code Playgroud)

我希望TestScopeClass.print_var()打印5,因为它可以读取classvarTestScopeClass身体.

为什么会这样?我应该在文档中阅读什么才能了解它.

python scope python-3.x

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

将混合嵌套列表转换为嵌套元组

如果我有

easy_nested_list = [['foo', 'bar'], ['foofoo', 'barbar']]
Run Code Online (Sandbox Code Playgroud)

并希望有

(('foo', 'bar'), ('foofoo', 'barbar'))
Run Code Online (Sandbox Code Playgroud)

我可以

tuple(tuple(i) for i in easy_nested_list)
Run Code Online (Sandbox Code Playgroud)

但如果我有

mixed_nested_list = [['foo', 'bar'], ['foofoo', ['foo', 'bar']],'some', 2, 3]
Run Code Online (Sandbox Code Playgroud)

并且想建立一个这样的元组,我不知道如何开始.

得到它会很高兴:

(('foo', 'bar'), ('foofoo', ('foo', 'bar')), 'some', 2, 3)
Run Code Online (Sandbox Code Playgroud)

第一个问题是Python将我的字符串转换为每个字符的元组.第二件事是我得到了

TypeError: 'int' object is not iterable
Run Code Online (Sandbox Code Playgroud)

python nested-lists python-3.x

0
推荐指数
1
解决办法
488
查看次数