Hướng dẫn how do you call one function from another in python? - làm thế nào để bạn gọi một chức năng từ một chức năng khác trong python?

Đưa ra một tệp python, chúng ta cần gọi một hàm trong nó được xác định trong bất kỳ tệp python nào khác. & Nbsp; ví dụ: & nbsp; & nbsp;
Example: 
 

Show

    Giả sử có một test test.py chứa định nghĩa của hàm displaytext (). & Nbsp;#test.py> def displayText (): Chúng ta cần gọi hàm displayText () trong bất kỳ tệp python nào khác sao cho bất cứ nơi nào chúng ta gọi là displayText () Hiển thị văn bản có trong đó. Điều này có thể được thực hiện bằng các mô -đun Python. & NBSP; & nbsp;
    #test.py>
    def displayText(): 
        print( “Geeks 4 Geeks!”)
    We need to call the function displayText() in any other Python file such that wherever we call displayText() function displays text present in it. This can be done using Python modules. 
     

    Approach: 
     

    1. Tạo một tệp Python chứa các chức năng cần thiết.
    2. Tạo một tệp Python khác và nhập tệp Python trước đó vào đó.
    3. Gọi các chức năng được xác định trong tệp đã nhập.

    Cách tiếp cận trên đã được sử dụng trong các ví dụ dưới đây: Ví dụ 1: một thử nghiệm tệp Python được tạo và nó chứa hàm displaytext (). & Nbsp;
    Example 1: A Python file test.py is created and it contains the displayText() function.
     

    Python3

    def displayText():

        print(

    7
    20
    0
    7
    20
    1

    Bây giờ một tệp python khác được tạo, gọi hàm displayText () được xác định trong test.py. & nbsp;
     

    Python3

    7
    20
    2
    7
    20
    3
    7
    20
    4
    7
    20
    5

    7
    20
    6

    Output: 
     

    Geeks 4 Geeks!

    Trong chương trình trên, tất cả các hàm được xác định trong tệp Test.py được nhập thì một hàm được gọi là modulusnumbers (). & nbsp;
    Example 2: A Python file calc.py is created containing addNumbers(), subractNumbers(), multiplyNumbers(), divideNumbers() and modulusNumbers().
     

    Python3

    def

    7
    20
    8

        print

    from file import *
    1
    from file import *
    2223
    from file import *
    4
    from file import *
    5

    def

    from file import *
    7

        print

    from file import *
    1
    from file import func1, func2, func3
    1
    from file import func1, func2, func3
    2
    from file import func1, func2, func3
    3
    from file import *
    5

    def

    from file import func1, func2, func3
    6

        print

    from file import *
    1
    Sum is  31
    Difference is  19
    Product is  150
    Division is  4.166666666666667
    Remainder is  1
    
    Geeks 4 Geeks!
    0
    from file import *
    3
    7
    20
    5

    def

    Sum is  31
    Difference is  19
    Product is  150
    Division is  4.166666666666667
    Remainder is  1
    
    Geeks 4 Geeks!
    5

        print

    from file import *
    1
    Sum is  31
    Difference is  19
    Product is  150
    Division is  4.166666666666667
    Remainder is  1
    
    Geeks 4 Geeks!
    9
    from file import *
    3def1
    from file import *
    5

    def def4

        print

    from file import *
    1def8
    from file import *
    3displayText():0

    Các hàm được xác định trong calc.py được gọi trong một tệp python khác. & Nbsp;
     

    Python3

    7
    20
    2 displayText():3
    7
    20
    4 displayText():5

    displayText():6displayText():7displayText():8displayText():9

    7
    20
    1

        1displayText():9displayText():8    4

    7
    20
    1

    Đầu ra: & nbsp;
     

    7
    20

    Trong chương trình trên, tất cả các chức năng được xác định trong calc.py không được nhập. & Nbsp; để nhập tất cả các chức năng được xác định trong tệp python: cú pháp: & nbsp;
    To import all the functions defined in a Python file:
    Syntax: 
     

    from file import *

    Để chỉ nhập các hàm yêu cầu được xác định trong tệp Python: Cú pháp: & NBSP;
    Syntax:
     

    from file import func1, func2, func3

    Ví dụ 3: & nbsp; Test Python Test.Py và calc.py bên dưới được tạo có các định nghĩa chức năng khác nhau. & NBSP; 
    The below Python files test.py and calc.py are created having various function definitions.
     

    Python3

    def displayText():

        print

    from file import *
    1print1
    7
    20
    1

    Python3

    def

    7
    20
    8

        print

    from file import *
    1
    from file import *
    2223
    from file import *
    4
    from file import *
    5

    def

    from file import *
    7

        print

    from file import *
    1
    from file import func1, func2, func3
    1
    from file import func1, func2, func3
    2
    from file import func1, func2, func3
    3
    from file import *
    5

    def

    from file import func1, func2, func3
    6

        print

    from file import *
    1
    Sum is  31
    Difference is  19
    Product is  150
    Division is  4.166666666666667
    Remainder is  1
    
    Geeks 4 Geeks!
    0
    from file import *
    3
    7
    20
    5

    def

    Sum is  31
    Difference is  19
    Product is  150
    Division is  4.166666666666667
    Remainder is  1
    
    Geeks 4 Geeks!
    5

        print

    from file import *
    1
    Sum is  31
    Difference is  19
    Product is  150
    Division is  4.166666666666667
    Remainder is  1
    
    Geeks 4 Geeks!
    9
    from file import *
    3def1
    from file import *
    5

    def def4

        print

    from file import *
    1def8
    from file import *
    3displayText():0

    Các hàm được xác định trong calc.py được gọi trong một tệp python khác. & Nbsp;
     

    Python3

    7
    20
    2 displayText():3
    7
    20
    4 displayText():5

    Đầu ra: & nbsp;

    displayText():6

    7
    20
    37displayText():8
    7
    20
    39
    7
    20
    1

    7
    20
    41
    7
    20
    37displayText():8
    7
    20
    39
    7
    20
    1

        1

    7
    20
    37displayText():8
    7
    20
    39
    7
    20
    1

    Trong chương trình trên, tất cả các chức năng được xác định trong calc.py không được nhập. & Nbsp; để nhập tất cả các chức năng được xác định trong tệp python: cú pháp: & nbsp;

    7
    20
    56
    7
    20
    37displayText():8
    7
    20
    39
    7
    20
    1

    7
    20
    6

    Output: 
     

    Sum is  31
    Difference is  19
    Product is  150
    Division is  4.166666666666667
    Remainder is  1
    
    Geeks 4 Geeks!

    Để chỉ nhập các hàm yêu cầu được xác định trong tệp Python: Cú pháp: & NBSP;