python缩进可以嵌套吗_一行Python代码可以知道其缩进嵌套级别吗?

本文探讨了如何实现一个自定义的`print`函数,该函数能够根据代码的嵌套级别输出相应的缩进。通过解析和计数Python源代码中的`INDENT`和`DEDENT`令牌,可以确定调用`get_nesting_level()`函数时的当前缩进级别。这种方法对于创建更易于阅读的输出非常有用,特别是在处理复杂代码结构时。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

From something like this:

print(get_indentation_level())

print(get_indentation_level())

print(get_indentation_level())

I would like to get something like this:

1

2

3

Can the code read itself in this way?

All I want is the output from the more nested parts of the code to be more nested. In the same way that this makes code easier to read, it would make the output easier to read.

Of course I could implement this manually, using e.g. .format(), but what I had in mind was a custom print function which would print(i*' ' + string) where i is the indentation level. This would be a quick way to make readable output on my terminal.

Is there a better way to do this which avoids painstaking manual formatting?

解决方案

If you want indentation in terms of nesting level rather than spaces and tabs, things get tricky. For example, in the following code:

if True:

print(

get_nesting_level())

the call to get_nesting_level is actually nested one level deep, despite the fact that there is no leading whitespace on the line of the get_nesting_level call. Meanwhile, in the following code:

print(1,

2,

get_nesting_level())

the call to get_nesting_level is nested zero levels deep, despite the presence of leading whitespace on its line.

In the following code:

if True:

if True:

print(get_nesting_level())

if True:

print(get_nesting_level())

the two calls to get_nesting_level are at different nesting levels, despite the fact that the leading whitespace is identical.

In the following code:

if True: print(get_nesting_level())

is that nested zero levels, or one? In terms of INDENT and DEDENT tokens in the formal grammar, it's zero levels deep, but you might not feel the same way.

If you want to do this, you're going to have to tokenize the whole file up to the point of the call and count INDENT and DEDENT tokens. The tokenize module would be very useful for such a function:

import inspect

import tokenize

def get_nesting_level():

caller_frame = inspect.currentframe().f_back

filename, caller_lineno, _, _, _ = inspect.getframeinfo(caller_frame)

with open(filename) as f:

indentation_level = 0

for token_record in tokenize.generate_tokens(f.readline):

token_type, _, (token_lineno, _), _, _ = token_record

if token_lineno > caller_lineno:

break

elif token_type == tokenize.INDENT:

indentation_level += 1

elif token_type == tokenize.DEDENT:

indentation_level -= 1

return indentation_level

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值