Hướng dẫn python import two functions with same name - python nhập hai hàm có cùng tên

Hãy để tôi tạo một mô -đun với hai chức năng và thêm nhân tôi gọi mô -đun này là

   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
8
I call this module as
   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
8

   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
9 bao gồm mã sau:

     # prog1.py
def add(x,y,z):
    return (x+y+z)
def multiply(x,y):
    return (x * y) 

Hãy để tôi tạo một mô -đun khác với hai hàm và tên các hàm là: Thêm và chia.

Tôi gọi mô -đun này là

 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
0

   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)

Trong một chương trình khác

 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
1, tôi nhập hai mô -đun này như sau:

 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)

Loại tuyên bố nhập khẩu này sẽ loại bỏ sự mơ hồ. Nó hoạt động tốt. Cả hai cuộc gọi chức năng sẽ được thực thi và chúng tôi nhận được kết quả.

#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)

Ở đây người phiên dịch sẽ xem xét cái mới nhất, từ

 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
2
from
 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
2

Chương trình trên sẽ đưa ra lỗi sau:

Traceback (most recent call last):
  File "prog4.py", line 4, in 
    a = add(10,20,30)
TypeError: add() takes 2 positional arguments but 3 were given
#prog5.py
from prog2 import add
from prog1 import add
b = add(10,20)
print("addition from prog2", b)
a = add(10,20,30)
print("addition  from prog1",a)
Traceback (most recent call last):
  File "prog5.py", line 7, in 
    b = add(10,20)
TypeError: add() missing 1 required positional argument: 'z'

Ở đây chương trình trên đang xem xét nhập khẩu mới nhất từ ​​

 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
3

Chúng ta phải cẩn thận nếu hai mô -đun có cùng chức năng với các tham số khác nhau trong khi nhập.

Tôi đã bắt đầu học Python và viết một ứng dụng thực hành. Cấu trúc thư mục trông giống như

src
 |
 --ShutterDeck
    |
    --Helper
       |
       --User.py -> class User
    --Controller
       |
       --User.py -> class User

Thư mục

 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
4 là trong
 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
5. Trong một tệp khác, hãy nói
 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
6, tôi muốn truy cập cả hai lớp
 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
7. Tôi làm nó như thế nào.

Tôi đã thử sử dụng những điều sau nhưng nó thất bại:

import cherrypy
from ShutterDeck.Controller import User
from ShutterDeck.Helper import User

class Root:
  @cherrypy.expose
  def index(self):
    return 'Hello World'

u1=User.User()
u2=User.User()

Điều đó chắc chắn là mơ hồ. Cách khác (C ++ để làm điều đó) theo cách mà tôi có thể nghĩ là

import cherrypy
from ShutterDeck import Controller
from ShutterDeck import Helper

class Root:

  @cherrypy.expose
  def index(self):
    return 'Hello World'

u1=Controller.User.User()
u2=Helper.User.User()

Nhưng khi tập lệnh trên được chạy, nó sẽ gây ra lỗi sau

u1=Controller.User.User()
AttributeError: 'module' object has no attribute 'User'

Tôi không thể tìm ra lý do tại sao nó bị lỗi? Các thư mục

 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
8,
 #prog3.py 
import prog1
import prog2
a = prog1.add(10,20,30)
print("addition  from prog1",a)
b = prog2.add(10,20)
print("addition from prog2", b)
9 và
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
0 có
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
1 trong đó.

Gọi các chức năng từ các tệp khác

Các chức năng do người dùng xác định có thể được gọi từ các tệp khác. Một hàm có thể được gọi và chạy trong một tệp khác với tệp nơi xác định hàm.

Nếu một tệp mới được gọi là myfunes.py được tạo và chứa hai định nghĩa hàm,

#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
2 và
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
3, các hàm
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
2 và
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
3 có thể được sử dụng bởi một tập lệnh riêng biệt miễn là tên tệp và tên hàm được nhập trong tập lệnh riêng biệt trước tiên. Điều cần thiết là tệp chứa các định nghĩa hàm kết thúc trong phần mở rộng .py. Không có phần mở rộng .py, tệp nơi các chức năng được xác định không thể được nhập. Bên trong tệp myfuctions.py, hai chức năng được xác định bằng mã bên dưới.myfunctions.py is created and contains two function definitions,
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
2 and
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
3, the functions
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
2 and
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
3 can be used by a separate script as long as the file and function names are imported in the separate script first. It is essential that the file which contains the function definitions ends in the .py extension. Without a .py extension, the file where the functions are defined can not be imported. Inside the file myfuctions.py, two functions are defined using the code below.

   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
0

Tệp này, myfunes.py có thể được nhập vào một tập lệnh khác (một tệp .py khác) hoặc máy tính xách tay Jupyter.myfunctions.py can be imported into another script (another .py file), or Jupyter Notebook.

Hãy nhớ rằng tệp chứa các định nghĩa chức năng và tệp gọi các hàm phải nằm trong cùng một thư mục.

Để sử dụng các chức năng được viết trong một tệp bên trong một tệp khác bao gồm dòng nhập,

#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
6. Lưu ý rằng mặc dù tên tệp phải chứa phần mở rộng .py,
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
7 không được sử dụng như một phần của tên tệp trong quá trình nhập..py extension,
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
7 is not used as part of the filename during import.

Cú pháp chung để nhập và gọi hàm từ một tệp riêng biệt ở bên dưới:

   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
1

Một ví dụ sử dụng cú pháp này với tệp myfifts.py và hàm

#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
2 ở bên dưới:myfunctions.py file and the function
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
2 is below:

In [1]:

   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
2

Nhiều chức năng có thể được nhập từ cùng một tệp bằng cách tách các chức năng được nhập với dấu phẩy. Cú pháp chung để nhập và gọi nhiều chức năng từ cùng một tệp là bên dưới:
   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
3

Một ví dụ sử dụng cú pháp này với tệp myfifts.py và các chức năng

#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
2 và
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
3 dưới đây:myfunctions.py file and the functions
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
2 and
#prog4.py
from prog1 import add
from prog2 import add

a = add(10,20,30)
print("addition  from prog1",a)

b = add(10,20)
print("addition from prog2", b)
3 is below:

In [2]:

   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
4

Một cách khác để nhập và sử dụng các chức năng từ myFunctions.py vào một tập lệnh khác hoặc máy tính xách tay Jupyter là nhập toàn bộ tệp myfifts.py với
Traceback (most recent call last):
  File "prog4.py", line 4, in 
    a = add(10,20,30)
TypeError: add() takes 2 positional arguments but 3 were given
1, sau đó gọi các chức năng với cú pháp bên dưới.
   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
5myfunctions.py into another script or Jupyter notebook is to import the entire myfunctions.py file with
Traceback (most recent call last):
  File "prog4.py", line 4, in 
    a = add(10,20,30)
TypeError: add() takes 2 positional arguments but 3 were given
1, then call the functions with the syntax below.
   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
5

Một ví dụ sử dụng cú pháp này với tệp myfifts.py bên dưới.myfunctions.py file is below.

In [3]:

   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
6

In [4]:

   # prog2.py
def add(x,y):
    return (x+y)
def divide(x,y):
    return (x / y)
7

Làm thế nào tôi có thể nhập hai mô -đun có cùng tên?

Để nhập hai lớp có cùng tên, hãy sử dụng từ khóa AS để đổi tên một hoặc cả hai lần nhập, ví dụ:Nhập {nhân viên dưới dạng nhân viên2} từ './anoth-file-2.use the as keyword to rename one or both of the imports, e.g. import {Employee as Employee2} from './another-file-2.

Hai mô -đun có thể có chức năng cùng tên không?

Nó hoạt động tốt.Cả hai cuộc gọi chức năng sẽ được thực thi và chúng tôi nhận được kết quả.Chúng ta phải cẩn thận nếu hai mô -đun có cùng chức năng với các tham số khác nhau trong khi nhập.. Both the function calls would be executed and we get the results. We have to be careful if two modules has same function with different parameters while importing.

Các chức năng có thể có cùng tên Python không?

Python không hỗ trợ quá tải chức năng.Khi chúng tôi xác định nhiều hàm cùng tên, phần sau luôn ghi đè trước và do đó, trong không gian tên, sẽ luôn có một mục nhập duy nhất so với mỗi tên hàm.. When we define multiple functions with the same name, the later one always overrides the prior and thus, in the namespace, there will always be a single entry against each function name.

Điều gì xảy ra nếu bạn xác định hai hàm trong một mô -đun có cùng tên?

Khi bạn xác định một hàm mới có cùng tên với hàm được xác định trước đó, tên hàm hiện được liên kết với đối tượng hàm mới và đối tượng hàm cũ được thu thập lại bởi Trình thu gom rác.the function name is now bound to the new function object, and the old function object is reclaimed by the garbage collector.