函数中的缩进没有按预期工作 [英] Indentation in function does not work as expected

查看:41
本文介绍了函数中的缩进没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些对我来说没有意义的东西.我希望有人能解释一下.

def test(word):总计 = 0对于 l 字词:打印 l测试('蟒蛇')

结果是p y th o n"(每个字母都出现在自己的行上).

def test(word):总计 = 0对于 l 字词:打印 l总回报测试('蟒蛇')

结果只是'p'.

为什么添加一个return 语句会有这种效果?两个代码块不应该做同样的事情吗?这是否意味着它只通过 for 循环一次,然后执行 return 语句?

解决方案

获得所见行为的唯一方法是将 return total 缩进为 for 循环.如果它在您的编辑器中看起来不是那样,那是因为您在 return 行中使用了 TAB 字符:

<预><代码>>>>code_as_posted = '''\...定义测试(字):... 总数 = 0... 对于 l in word:... 打印 l... 总回报...'''>>>code_as_posted.splitlines()[-1]'\t返回总数'

看到那里的 \t 字符转义了吗?这是一个制表符,最多可扩展到8 个空格 当被 Python 解释时;请参阅 Python 参考文档中的缩进:

<块引用>

首先,制表符被一到八个空格替换(从左到右),这样直到并包括替换的字符总数是八的倍数(这与 Unix 使用的规则相同).第一个非空白字符之前的空格总数决定了该行的缩进.

由于 return 提前退出循环,您只能看到打印的 'p' 字符.

您的编辑器和 Stack Overflow Markdown 实现都将选项卡显示为 四个 空格,因此在这里很难发现这个特定错误.使用 -tt 命令行开关运行 Python 在混合使用制表符和空格时引发异常.

您需要将编辑器配置为将制表符扩展为空格,或仅使用制表符.不要混合两种风格.Python 风格指南 强烈建议您只使用空格:

<块引用>

空格是首选的缩进方法.

制表符应仅用于与已使用制表符缩进的代码保持一致.

许多编辑器还可以使选项卡显式可见.在 Sublime Text 中,当您选择文本时,空格显示为暗点,制表符显示为一行:

I found something that doesn't make sense to me. I am hoping that someone can explain it.

def test(word):
    total = 0
    for l in word:
        print l

test('python')

The result is 'p y t h o n' (each letter appears on its own line).

def test(word):
    total = 0
    for l in word:
        print l
    return total

test('python')

The result is just 'p'.

How come adding a return statement has this effect? Shouldn't both blocks of code do that same thing? Does this mean that it goes through the for loop only once and then executes the return statement?

解决方案

The only way you'd get the behaviour you see is if you indented return total to be part of the for loop. If it doesn't look like that in your editor, it's because you have used a TAB character for your return line:

>>> code_as_posted = '''\
... def test(word):
...     total = 0
...     for l in word:
...         print l
...     return total
... '''
>>> code_as_posted.splitlines()[-1]
'\treturn total'

See that \t character escape there? That's a tab character, which expands to up to 8 spaces when interpreted by Python; see Indentation in the Python reference documentation:

First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line’s indentation.

As the return exits the loop early, you only ever see the 'p' character printed.

Both your editor and the Stack Overflow Markdown implementation display tabs as four spaces instead, so it was really hard to spot this specific error here. Run Python with the -tt command line switch to raise exceptions on mixed tab-and-spaces use.

You need to configure your editor to expand tabs to spaces, or use tabs exclusively. Don't mix the two styles. The Python Style Guide strongly recommends you use spaces only:

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

Many editors can also make tabs explicitly visible. In Sublime Text, when you select text, spaces are shown as dimmed dots, tabs as a line:

这篇关于函数中的缩进没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆