Hướng dẫn python positional-only arguments - đối số chỉ vị trí python

Trong bài học này, bạn sẽ tìm hiểu về cách thêm các đối số chỉ có vị trí vào các chức năng của bạn trong Python 3.8.positional-only arguments to your functions in Python 3.8.

Hàm tích hợp

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0 có thể được sử dụng để chuyển đổi các chuỗi văn bản và số thành các đối tượng nổi. Xem xét ví dụ sau:

>>>

>>> float["3.8"]
3.8

>>> help[float]
class float[object]
 |  float[x=0, /]
 |  
 |  Convert a string or number to a floating point number, if possible.

[...]

Nhìn kỹ vào chữ ký của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0. Lưu ý rằng chém [
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2] sau tham số. Nó có nghĩa là gì?

Hóa ra trong khi một tham số của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0 được gọi là
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
4, bạn không được phép sử dụng tên của nó:

>>>

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments

Nhìn kỹ vào chữ ký của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0. Lưu ý rằng chém [
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2] sau tham số. Nó có nghĩa là gì?positional-only arguments were only possible for built-in functions. There was no easy way to specify that arguments should be positional-only in your own functions:

>>>

>>> def incr[x]:
...     return x + 1
...
>>> incr[3.8]
4.8

>>> incr[x=3.8]
4.8

Nhìn kỹ vào chữ ký của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0. Lưu ý rằng chém [
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2] sau tham số. Nó có nghĩa là gì?

>>>

>>> def incr[x, /]:
...     return x + 1
...
>>> incr[3.8]
4.8

>>> incr[x=3.8]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: incr[] got some positional-only arguments passed as
           keyword arguments: 'x'

Nhìn kỹ vào chữ ký của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0. Lưu ý rằng chém [
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2] sau tham số. Nó có nghĩa là gì?

>>>

>>> def greet[name, /, greeting="Hello"]:
...     return f"{greeting}, {name}"
... 
>>> greet["Christopher"]
'Hello, Christopher'

>>> greet["Christopher", greeting="Awesome job"]
'Awesome job, Christopher'

>>> greet[name="Christopher", greeting="Did it work?"]
Traceback [most recent call last]:
  File "", line 1, in 
    greet[name="Christopher", greeting="Did it work?"]
TypeError: greet[] got some positional-only arguments passed as
           keyword arguments: 'name'

Nhìn kỹ vào chữ ký của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0. Lưu ý rằng chém [
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2] sau tham số. Nó có nghĩa là gì?

Hóa ra trong khi một tham số của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0 được gọi là
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
4, bạn không được phép sử dụng tên của nó:

Khi sử dụng

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0, bạn chỉ được phép chỉ định các đối số theo vị trí, không phải bằng từ khóa. Trước Python 3.8, các đối số chỉ có vị trí như vậy chỉ có thể cho các chức năng tích hợp. Không có cách nào dễ dàng để chỉ định rằng các đối số phải chỉ thuộc về các chức năng của riêng bạn:

Trong Python 3.8, bạn có thể sử dụng

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2 để biểu thị rằng tất cả các đối số trước khi nó phải được chỉ định theo vị trí. Bạn có thể viết lại
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
7 để chỉ chấp nhận các đối số vị trí:

Bằng cách thêm

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2 sau
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
4, bạn chỉ định rằng
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
4 là một đối số chỉ có vị trí. Bạn có thể kết hợp các đối số thông thường với các đối số chỉ có vị trí bằng cách đặt các đối số thông thường sau khi chém:
nicely complement keyword-only arguments. In any version of Python 3, you can specify keyword-only arguments using the star
>>> def incr[x]:
...     return x + 1
...
>>> incr[3.8]
4.8

>>> incr[x=3.8]
4.8
6. Any argument after
>>> def incr[x]:
...     return x + 1
...
>>> incr[3.8]
4.8

>>> incr[x=3.8]
4.8
7 must be specified using a keyword:

>>>

>>> def to_fahrenheit[*, celsius]:
...     return 32 + celsius * 9 / 5
... 
>>> to_fahrenheit[40]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: to_fahrenheit[] takes 0 positional arguments but 1 was given

>>> to_fahrenheit[celsius=40]
104.0

Nhìn kỹ vào chữ ký của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0. Lưu ý rằng chém [
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2] sau tham số. Nó có nghĩa là gì?

Hóa ra trong khi một tham số của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0 được gọi là
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
4, bạn không được phép sử dụng tên của nó:

>>>

>>> def headline[text, /, border="~", *, width=50]:
...     return f" {text} ".center[width, border]
...

Nhìn kỹ vào chữ ký của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0. Lưu ý rằng chém [
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2] sau tham số. Nó có nghĩa là gì?

>>>

>>> headline["Positional-only Arguments"]
'~~~~~~~~~~~ Positional-only Arguments ~~~~~~~~~~~~'

>>> headline[text="This doesn't work!"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: headline[] got some positional-only arguments passed as
           keyword arguments: 'text'

Hóa ra trong khi một tham số của

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0 được gọi là
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
4, bạn không được phép sử dụng tên của nó:

>>>

>>> headline["Python 3.8", "="]
'=================== Python 3.8 ==================='

>>> headline["Real Python", border=":"]
':::::::::::::::::: Real Python :::::::::::::::::::'

Khi sử dụng

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
0, bạn chỉ được phép chỉ định các đối số theo vị trí, không phải bằng từ khóa. Trước Python 3.8, các đối số chỉ có vị trí như vậy chỉ có thể cho các chức năng tích hợp. Không có cách nào dễ dàng để chỉ định rằng các đối số phải chỉ thuộc về các chức năng của riêng bạn:

>>>

>>> headline["Python", "@", width=38]
'@@@@@@@@@@@@@@@ Python @@@@@@@@@@@@@@@'

>>> headline["Python", "@", 38]
Traceback [most recent call last]:
  File "", line 1, in 
    headline["Python", "@", 38]
TypeError: headline[] takes from 1 to 2 positional arguments
           but 3 were given

Trong Python 3.8, bạn có thể sử dụng

>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
2 để biểu thị rằng tất cả các đối số trước khi nó phải được chỉ định theo vị trí. Bạn có thể viết lại
>>> float[x="3.8"]
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: float[] takes no keyword arguments
7 để chỉ chấp nhận các đối số vị trí:

  • Bằng cách thêm
    >>> float[x="3.8"]
    Traceback [most recent call last]:
      File "", line 1, in 
    TypeError: float[] takes no keyword arguments
    
    2 sau
    >>> float[x="3.8"]
    Traceback [most recent call last]:
      File "", line 1, in 
    TypeError: float[] takes no keyword arguments
    
    4, bạn chỉ định rằng
    >>> float[x="3.8"]
    Traceback [most recent call last]:
      File "", line 1, in 
    TypeError: float[] takes no keyword arguments
    
    4 là một đối số chỉ có vị trí. Bạn có thể kết hợp các đối số thông thường với các đối số chỉ có vị trí bằng cách đặt các đối số thông thường sau khi chém:
    provides an in-depth discussion on the
    >>> float[x="3.8"]
    Traceback [most recent call last]:
      File "", line 1, in 
    TypeError: float[] takes no keyword arguments
    
    2 notation.
  • Trong
    >>> def incr[x]:
    ...     return x + 1
    ...
    >>> incr[3.8]
    4.8
    
    >>> incr[x=3.8]
    4.8
    
    1, dấu gạch chéo được đặt từ
    >>> def incr[x]:
    ...     return x + 1
    ...
    >>> incr[3.8]
    4.8
    
    >>> incr[x=3.8]
    4.8
    
    2 và
    >>> def incr[x]:
    ...     return x + 1
    ...
    >>> incr[3.8]
    4.8
    
    >>> incr[x=3.8]
    4.8
    
    3. Điều này có nghĩa là
    >>> def incr[x]:
    ...     return x + 1
    ...
    >>> incr[3.8]
    4.8
    
    >>> incr[x=3.8]
    4.8
    
    2 là một đối số chỉ có vị trí, trong khi
    >>> def incr[x]:
    ...     return x + 1
    ...
    >>> incr[3.8]
    4.8
    
    >>> incr[x=3.8]
    4.8
    
    3 là một đối số thông thường có thể được truyền theo vị trí hoặc từ khóa.
    gives more details on Python positional-only parameters.

Bài Viết Liên Quan

Chủ Đề