在模块内运行Python内部函数。

huangapple go评论97阅读模式
英文:

Python Run Inner Function Within Module

问题

You can call an inner function within a Python module by first calling the outer function and then the inner function, like this:

  1. import custom_module
  2. custom_module.outerFunction() # Call the outer function
  3. custom_module.outerFunction().innerFunction() # Call the inner function within the outer function

Make sure to use the correct module name (in your case, "custom_module") when importing and calling functions.

英文:

How can I call an inner function within a Python module?

I have created a Python module. Within this module, there is a function. Within this function, there is a nested function. I saved the Python file with the name custom_module.py.

I have then called this module using the statement import custom_module.

I have then tried to call the nested function. I did this using the statement custom_module.outer_function().inner_module().

This has not called the function correctly. How could I call the inner function from within a Python module, or will I need to create separate functions and not use nested functions.

I do not always want to call the inner functions when I run the outer function.

An example of a file named module.py:

  1. def outerFunction():
  2. print("This is the outer function")
  3. def innerFunction():
  4. print("This is the inner function")

An example of the main file:

  1. import module
  2. module.outerFunction().innerFunction()

答案1

得分: 2

The code you provided is explaining the concept of scope in Python programming. Here's the translation of the code explanation:

  1. 类似以下的情况
  2. ```python
  3. def foo():
  4. def bar():
  5. print("foo bar")
  6. bar()

在这种情况下,函数 bar 局部作用域于函数 foo,不能在该作用域之外调用,除非将其从该作用域返回到外部作用域。

  1. >>> def foo():
  2. ... def bar():
  3. ... print("foo bar")
  4. ... bar()
  5. ... return bar
  6. ...
  7. >>> bar = foo()
  8. foo bar
  9. >>> bar()
  10. foo bar

这与您尝试在函数之外的作用域中访问局部作用域的变量时所见到的情况是一样的。

  1. def foo():
  2. x = 42
  3. print(x)

您不应该期望能够在函数 foo 的作用域之外访问 x

  1. <details>
  2. <summary>英文:</summary>
  3. Something like the following presumably.

def foo():
def bar():
print("foo bar")
bar()

  1. The function `bar` in this case is locally scoped to the function `foo` can cannot be called outside of that scope, _unless_ it is returned from that scope to the outer scope.

>>> def foo():
... def bar():
... print("foo bar")
... bar()
... return bar
...
>>> bar = foo()
foo bar
>>> bar()
foo bar

  1. It&#39;s the same thing you&#39;d see if you wanted to access a variable locally scoped to a function outside of the function&#39;s scope.

def foo():
x = 42
print(x)

  1. You would not expect to be able to access `x` except within the scope of the function `foo`.
  2. </details>
  3. # 答案2
  4. **得分**: -2
  5. 你可以在外部函数内使用if语句。这将允许您只运行需要运行的内部函数。例如:
  6. ```Python
  7. def outerFunction(parameter):
  8. print("这是外部函数")
  9. def innerFunction1():
  10. print("这是第一个内部函数")
  11. def innerFunction2():
  12. print("这是第二个内部函数")
  13. if parameter == "firstFunction":
  14. innerFunction1()
  15. elif parameter == "secondFunction":
  16. innerFunction2()
  17. outerFunction("secondFunction")

上述代码的输出将是:这是第二个内部函数

英文:

You could use if statements within the outer function. This will allow you to only run the inner functions which need to be run. For example:

  1. def outerFunction(parameter):
  2. print(&quot;This is the outer function&quot;)
  3. def innerFunction1():
  4. print(&quot;This is the first inner function&quot;)
  5. def innerFunction2():
  6. print(&quot;This is the second inner function&quot;)
  7. if parameter == &quot;firstFunction&quot;:
  8. innerFunction1()
  9. else if parameter == &quot;secondFunction&quot;:
  10. innerFunction2()
  11. outerFunction(secondFunction)

The output of the above code would be This is the second inner function

huangapple
  • 本文由 发表于 2023年5月17日 23:22:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定