【问题标题】:how to send nested functions data to another function in python如何将嵌套函数数据发送到python中的另一个函数
【发布时间】:2021-08-01 23:48:40
【问题描述】:

我想在世界函数中显示一个变量的值。

def base():
    
    print("Hello World")
    
    def child():
        a = 10
        return a
    
def world():
    num = base.child() # error -------
    print(num)
    
world()```

【问题讨论】:

  • base 在表达式base.child() 中代表什么?标量值、对象、类、方法还是什么?
  • base 是一个函数
  • 函数是否有属性'child'?
  • 基函数有子函数。

标签: python python-3.x django function nested-function


【解决方案1】:

我建议在 base 中调用 child。

def base():
    
    print("Hello World")
    def child():
        a = 10
        return a
    return child()
def world():
    num = base()
    print(num)
    
world()

【讨论】:

  • 现在它可以工作了,但我想要另一种方式(不要从基函数返回子函数)
  • 我认为没有任何方法可以从外部访问内部函数。这就是重点。正如@BuddyBob 所建议的那样,您所能期望的最好的结果就是访问内部函数的results。您可能会发现此信息。 Python Inner Functions: What Are They Good For?
猜你喜欢
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多