Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';有人考虑在python中嵌套吗?_Python - Fatal编程技术网

什么';有人考虑在python中嵌套吗?

什么';有人考虑在python中嵌套吗?,python,Python,我一直认为嵌套是将一个对象嵌入到同一类型的另一个对象中 但是最近我被告知嵌套也意味着嵌入不同类型的对象,所以下面有三个嵌套级别 def my_function(): # first level of nesting ??? while expr: # second level of nesting ??? if expr: # third level of nesting ??? do_somethi

我一直认为嵌套是将一个对象嵌入到同一类型的另一个对象中

但是最近我被告知嵌套也意味着嵌入不同类型的对象,所以下面有三个嵌套级别

def my_function():
    # first level of nesting ???
    while expr:
        # second level of nesting ???
        if expr:
            # third level of nesting ???
            do_something()
        else:
            # third level of nesting ???
            do_something_else()

我在网上搜索过,但找不到任何简明的答案。

嵌套至少有两种定义:

  • 将一个对象放在另一个对象中可以被视为嵌套(想想俄罗斯的嵌套玩偶),但它通常被称为合成。我猜这就是你所说的把一个物体放在另一个物体里面;尽管这些类型不要求是同一类型,而且通常不是,除非定义递归数据结构

  • 代码片段中显示的内容也可以被视为嵌套,因为在其他表达式/语句中有表达式/语句。考虑到它的Python,它也可以被认为是缩进的级别,因为缩进级别表示Python中的嵌套级别


嵌套意味着将一块代码放入另一块中。 以下是嵌套块的示例

if True:
    a = 0 
    a = a + 5 
    if a < 10: 
        a = 20

for i in [1, 2, 3]: 
    print i
    try:
        b = i + 10
    except:
        print 'bye'
如果为真:
a=0
a=a+5
如果a<10:
a=20
因为我在[1,2,3]中:
打印i
尝试:
b=i+10
除:
打印“再见”

在上面的代码中,如果一个<10
嵌套在块下,如果为True,则块嵌套在块下,与try相同,除了块嵌套在for循环的块下,这是缩进,而不是嵌套。首先,这些不是对象,它们不嵌入任何东西。这些都是函数中的语句。嵌套是指特定代码执行某些函数,并且包含在执行更广泛函数的代码中。因此,将语句放入函数中就是这样,所以这里没有嵌套,这同样适用于while或for循环?