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

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

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:

import custom_module
custom_module.outerFunction()  # Call the outer function
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:

def outerFunction():
    print("This is the outer function")
    def innerFunction():
        print("This is the inner function")

An example of the main file:

import module
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:

类似以下的情况

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

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

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

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

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

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


<details>
<summary>英文:</summary>

Something like the following presumably.

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


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


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)


You would not expect to be able to access `x` except within the scope of the function `foo`.



</details>



# 答案2
**得分**: -2

你可以在外部函数内使用if语句。这将允许您只运行需要运行的内部函数。例如:
```Python
def outerFunction(parameter):
    print("这是外部函数")
    def innerFunction1():
        print("这是第一个内部函数")
    def innerFunction2():
        print("这是第二个内部函数")
    if parameter == "firstFunction":
        innerFunction1()
    elif parameter == "secondFunction":
        innerFunction2()

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:

def outerFunction(parameter):
    print(&quot;This is the outer function&quot;)
    def innerFunction1():
        print(&quot;This is the first inner function&quot;)
    def innerFunction2():
        print(&quot;This is the second inner function&quot;)
    if parameter == &quot;firstFunction&quot;:
        innerFunction1()
    else if parameter == &quot;secondFunction&quot;:
        innerFunction2()

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:

确定