Hướng dẫn how many arguments can python function take? - Hàm python có thể nhận bao nhiêu đối số?

Trong Python 3.7 và mới hơn, không có giới hạn. Đây là kết quả của công việc được thực hiện trong vấn đề #27213 và vấn đề #12844; #27213 đã làm lại họ Opcodes

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
0 về hiệu suất và tính đơn giản [một phần của 3.6], giải phóng đối số opcode để chỉ mã hóa một số lượng đối số duy nhất và #12844 đã xóa kiểm tra thời gian biên dịch để ngăn mã có nhiều đối số hơn được biên dịch.

Vì vậy, theo 3.7, với opcode

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
1, giờ đây hoàn toàn không có giới hạn về số lượng đối số bạn có thể truyền trong việc sử dụng các đối số rõ ràng, hãy lưu số lượng có thể được lắp vào ngăn xếp [do đó bị ràng buộc bởi bộ nhớ của bạn]:

>>> import sys
>>> sys.version_info
sys.version_info[major=3, minor=7, micro=0, releaselevel='alpha', serial=2]
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
>>> exec["f[{}]".format[', '.join[map[str, range[2 ** 16]]]]]

Xin lưu ý rằng danh sách, bộ dữ liệu và từ điển được giới hạn trong các phần tử

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
2, vì vậy nếu hàm được gọi là
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
3 và/hoặc
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
4 tham số bắt tất cả thì đó bị hạn chế.

Đối với cú pháp cuộc gọi

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
3 và
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
4 [mở rộng đối số], không có giới hạn nào khác ngoài cùng giới hạn kích thước
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
7 đối với các loại tiêu chuẩn Python.

Trong các phiên bản trước Python 3.7, Cpython có giới hạn 255 đối số được thông qua rõ ràng trong một cuộc gọi:

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments

Giới hạn này được đặt ra bởi vì cho đến khi Python 3.5, opcode

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
8 đã quá tải đối số opcode để mã hóa cả số lượng đối số vị trí và từ khóa trên ngăn xếp, mỗi đối số được mã hóa trong một byte.

Một thói quen là một nhóm hướng dẫn được đặt tên thực hiện một số nhiệm vụ. Một thói quen luôn có thể được gọi cũng như được gọi là nhiều lần theo yêu cầu trong một chương trình nhất định. & NBSP;Routine is a named group of instructions performing some tasks. A routine can always be invoked as well as called multiple times as required in a given program.
 

Khi thói quen dừng lại, việc thực hiện ngay lập tức trở lại giai đoạn mà thói quen được gọi. Các thói quen như vậy có thể được xác định trước trong ngôn ngữ lập trình hoặc được thiết kế hoặc thực hiện bởi lập trình viên. Một chức năng là phiên bản Python của thói quen trong một chương trình. Một số chức năng được thiết kế để trả về các giá trị, trong khi các hàm khác được thiết kế cho các mục đích khác.Function is the Python version of the routine in a program. Some functions are designed to return values, while others are designed for other purposes.
We pass arguments in a function, we can pass no arguments at all, single arguments or multiple arguments to a function and can call the function multiple times.
Example:

Python

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
9
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
20
15
4
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
222
15
4444444

15
6
def functionName[**argument]
1
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
01
def functionName[**argument]
3
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
33

Geeks for Geeks
6

Output:

Geeks for Geeks

def functionName[**argument]
5
Geeks for Geeks
222

Python

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
9
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
20
15
4
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
222
15
4444444

Geeks for Geeks
9
Geeks for Geeks
2
Geeks for Geeks
3
Hello R2J !
2
Hello R2J !
3
Hello R2J !
4
Hello R2J !
3
Hello R2J !
6
Geeks for Geeks
5

15
6
def functionName[**argument]
1
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
01
def functionName[**argument]
3
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
33

Geeks 4 Geeks
1

Output:

Hello R2J !

def functionName[**argument]
5
Geeks for Geeks
222
Passing multiple arguments to a function in Python:

  • 15
    6
    def functionName[**argument]
    1
    >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    01
    def functionName[**argument]
    3
    >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    41
     

Python

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
9
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
20
15
4
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
222
15
4444444

15
6
def functionName[**argument]
1
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
01
def functionName[**argument]
3
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
33

def functionName[*argument]
5
def functionName[*argument]
6
def functionName[*argument]
7
def functionName[*argument]
8
def functionName[*argument]
7
def functionName[*argument]
6
Geeks for Geeks
5

  • Output:
Geeks 4 Geeks
  • def functionName[**argument]
    5
    Geeks for Geeks
    222
  • 15
    6
    def functionName[**argument]
    1
    >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    01
    def functionName[**argument]
    3
    >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    41
def functionName[*argument]
  • >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    45
    Hello R2J !
    9
    >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    47
     

Python

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
9
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
20
15
4
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
222
15
4444444

15
6
def functionName[**argument]
1
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
01
def functionName[**argument]
3
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
33

def functionName[**argument]
5
Geeks for Geeks
222

15
6
def functionName[**argument]
1
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
01
def functionName[**argument]
3
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
41

15
6
Geeks for Geeks
2
['argument2', 4]
['argument3', 'Geeks']
['argument1', 'Geeks']
2

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
45
Hello R2J !
9
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
47

  • Output:
15
  • >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    48
    Hello R2J !
    9
    def functionName[*argument]
    6
  • >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    51
    >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    522153
    Hello R2J !
    9
    ['argument2', 4]
    ['argument3', 'Geeks']
    ['argument1', 'Geeks']
    6
    >>> def f[*args, **kwargs]: pass
    ...
    >>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 1
    SyntaxError: more than 255 arguments
    
    13
def functionName[**argument]
  • Chương trình trên minh họa việc sử dụng số lượng biến của cả đối số không phải là Keyword và đối số từ khóa cũng như đối số không kích thích trong một hàm. Đối số không phải là luôn được sử dụng trước đối số dấu hoa thị đơn và đối số dấu hoa thị đơn luôn được sử dụng trước đối số kép trong một định nghĩa hàm. & NBSP;
     

Python

Một thói quen là một nhóm hướng dẫn được đặt tên thực hiện một số nhiệm vụ. Một thói quen luôn có thể được gọi cũng như được gọi là nhiều lần theo yêu cầu trong một chương trình nhất định. & NBSP;

Khi thói quen dừng lại, việc thực hiện ngay lập tức trở lại giai đoạn mà thói quen được gọi. Các thói quen như vậy có thể được xác định trước trong ngôn ngữ lập trình hoặc được thiết kế hoặc thực hiện bởi lập trình viên. Một chức năng là phiên bản Python của thói quen trong một chương trình. Một số chức năng được thiết kế để trả về các giá trị, trong khi các hàm khác được thiết kế cho các mục đích khác.

def functionName[**argument]
5
Geeks for Geeks
2
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
06

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
9
Geeks for Geeks
0

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
14
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
15
Hello R2J !
9
def functionName[*argument]
6
Geeks for Geeks
5

  • Output:
['argument2', 4]
['argument3', 'Geeks']
['argument1', 'Geeks']
  • Trong chương trình trên, số lượng các đối số từ khóa được chuyển đến hàm DisplayArgument [].

Dưới đây là một chương trình để minh họa tất cả các trường hợp trên để vượt qua nhiều đối số trong một hàm. & NBSP;

Python

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
9
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
20
15
4
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
222
15
4444444

15
6
Geeks for Geeks
2
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
28

15
6
def functionName[**argument]
1
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
01
def functionName[**argument]
3
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
33

def functionName[**argument]
5
Geeks for Geeks
222

15
6
def functionName[**argument]
1
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
01
def functionName[**argument]
3
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
41

def functionName[**argument]
5
Geeks for Geeks
2
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
06

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
45
Hello R2J !
9
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
47

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
48
Hello R2J !
9
def functionName[*argument]
6

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
51
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
522153
Hello R2J !
9
['argument2', 4]
['argument3', 'Geeks']
['argument1', 'Geeks']
6
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
13

>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
57
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
58
Hello R2J !
9
>>> def f[*args, **kwargs]: pass
...
>>> exec["f[{}]".format[', '.join[map[str, range[256]]]]]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 1
SyntaxError: more than 255 arguments
60
Geeks for Geeks
5

Output:

Welcome
to
Geeks
['agr4', 4]
['arg5', 'Geeks!']

Chương trình trên minh họa việc sử dụng số lượng biến của cả đối số không phải là Keyword và đối số từ khóa cũng như đối số không kích thích trong một hàm.Đối số không phải là luôn được sử dụng trước đối số dấu hoa thị đơn và đối số dấu hoa thị đơn luôn được sử dụng trước đối số kép trong một định nghĩa hàm. & NBSP;


Bài Viết Liên Quan

Chủ Đề