gpt4 book ai didi

Python 空闲 : How to type correct indentation?

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:12 28 4
gpt4 key购买 nike

麦克:特立独行 python :3.4

我尝试在 Python 的 IDLE 中测试 timeit 模块

import timeit

>>> timeit.timeit( "obj.method", """
class SomeClass:
def method(self):
pass
obj = SomeClass()
""")

当我尝试在 class SomeClass 的下一行键入 def method(self): 时,我点击了 Tab,它提示了一个显示我的 Document 目录中的文件的窗口。所以我改为按 Ctrl+Tab

但是还是报错:

Traceback (most recent call last):
File "<pyshell#26>", line 6, in <module>
""")
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/timeit.py", line 213, in timeit
return Timer(stmt, setup, timer).timeit(number)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/timeit.py", line 122, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 9
_t0 = _timer()
^
IndentationError: unindent does not match any outer indentation level

谁能解释一下:

  1. 如何修复它,以及
  2. 如何避免出现我的文档的提示?

谢谢!!!

最佳答案

试试这个:

>>> timeit.timeit( "obj.method", """
class SomeClass:
def method(self):
pass
obj = SomeClass()
""")

如果您在像这样的常规 python 模块中定义了一个带有额外缩进的类:

    class Hello:
pass

解释器会抛出 IndentationError。 timeit 方法似乎像解析常规 python 文件一样解析输入字符串,并出于类似原因抛出缩进错误。请注意,您的字符串在关键字“Class”之前以额外的(不必要的)缩进开头。换行没问题。此外,将空格与制表符混合也可能导致缩进错误。

我没有 mac,我无法重新创建“mydocument”问题,所以我无法在这方面帮助您 :(

关于Python 空闲 : How to type correct indentation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25468202/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com