📜  Python – 从另一个函数调用函数

📅  最后修改于: 2022-05-13 01:55:02.474000             🧑  作者: Mango

Python – 从另一个函数调用函数

先决条件: Python中的函数
在Python中,任何编写的函数都可以被另一个函数。请注意,这可能是将问题分解为小问题的最优雅的方法。在本文中,我们将学习如何借助多个示例从另一个函数调用定义的函数。

调用和被调用函数?
调用另一个函数的函数称为Calling 函数 ,另一个函数调用的函数称为Called 函数。

函数执行是如何工作的?
在函数调用的执行过程中使用堆栈数据结构。每当调用一个函数时,调用函数就会被压入堆栈并执行被调用的函数。当被调用函数完成执行并返回时,调用函数将从堆栈中弹出并执行。只有在被调用函数执行完成时,调用函数执行才会完成。

在下图中。 Main函数对Function1进行函数调用,此时 Main函数的状态存储在 Stack 中,当函数 1返回时继续执行 Main函数。现在Fucntion1调用Function2Function1的状态存储在堆栈中,当函数 2返回时, 函数 1的执行将继续。

考虑下面的函数调用示例。函数SumOfSquares函数调用函数Square,它返回数字的平方。

Python3
# Python code to demonstrate calling the
# function from another function
 
def Square(X):
    # computes the Square of the given number
    # and return to the caller function
    return (X * X)
 
def SumofSquares(Array, n):
 
    # Initialize variable Sum to 0. It stores the
    # Total sum of squares of the array of elements
    Sum = 0
    for i in range(n):
 
        # Square of Array[i] element is stored in SquaredValue
        SquaredValue = Square(Array[i])
 
        # Cumulative sum is stored in Sum variable
        Sum += SquaredValue
    return Sum
 
# Driver Function
Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = len(Array)
 
# Return value from the function
# Sum of Squares is stored in Total
Total = SumofSquares(Array, n)
print("Sum of the Square of List of Numbers:", Total)


Python3
'''
Call a function from within another function
in the same class in Python
'''
 
class Main:
 
    # constructor of Main class
    def __init__(self):
        # Initialization of the Strings
        self.String1 ="Hello"
        self.String2 ="World"
 
    def Function1(self):
        # calling Function2 Method
        self.Function2()
        print("Function1 : ", self.String2)
        return
 
    def Function2(self):
        print("Function2 : ", self.String1)
        return
 
# Instance of Class Main
Object = Main()
 
# Calling Function1
Object.Function1()


Python3
# Python code to demonstrate calling parent class
# method from the child class method
 
class Parent:
 
    # constructor of Parent class
    def __init__(self):
        # Initialization of the Strings
        self.String1 ="Hello"
        self.String2 ="World"
 
    def Function2(self):
        print("Function2 : ", self.String1)
        return
 
# Child class is inheriting from Parent class
class Child(Parent):
 
    def Function1(self):
        # calling Function2 Method in parent class
        self.Function2()
        print("Function1 : ", self.String2)
        return  
 
### Instance of Parent class
Object1 = Parent()
 
### Instance of Child class
Object2 = Child()
 
# Calling Function1 using Child class instance
Object2.Function1()


输出 :

Sum of the Square of List of Numbers: 385 

从同一类中的另一个函数调用函数-
在下面的示例中,类方法 Function1 从类中调用方法 Function2。

Python3

'''
Call a function from within another function
in the same class in Python
'''
 
class Main:
 
    # constructor of Main class
    def __init__(self):
        # Initialization of the Strings
        self.String1 ="Hello"
        self.String2 ="World"
 
    def Function1(self):
        # calling Function2 Method
        self.Function2()
        print("Function1 : ", self.String2)
        return
 
    def Function2(self):
        print("Function2 : ", self.String1)
        return
 
# Instance of Class Main
Object = Main()
 
# Calling Function1
Object.Function1()

输出 :

Function2 :  Hello
Function1 :  World

函数子类函数调用父类函数-
考虑下面的例子,子类方法调用父类方法。子类继承父类的属性。

Python3

# Python code to demonstrate calling parent class
# method from the child class method
 
class Parent:
 
    # constructor of Parent class
    def __init__(self):
        # Initialization of the Strings
        self.String1 ="Hello"
        self.String2 ="World"
 
    def Function2(self):
        print("Function2 : ", self.String1)
        return
 
# Child class is inheriting from Parent class
class Child(Parent):
 
    def Function1(self):
        # calling Function2 Method in parent class
        self.Function2()
        print("Function1 : ", self.String2)
        return  
 
### Instance of Parent class
Object1 = Parent()
 
### Instance of Child class
Object2 = Child()
 
# Calling Function1 using Child class instance
Object2.Function1()

输出 :

Function2 :  Hello
Function1 :  World