推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
SlipStupig
V2EX  ›  Python

遇到了 Python 的少见语法问题

  •  
  •   SlipStupig · Feb 1, 2021 · 2850 views
    This topic created in 1952 days ago, the information mentioned may be changed or developed.

    在看一个 numpy 的代码时候遇到了一个,平时见得比较少的操作符符号--“@”, 实例如下:

    a1 = np.array([[1,2,3],[4,5,6]])
    b1 = np.array([1,2,3])
    a1 @ b1
    
    array([14, 32])
    

    我知道 @xxx 可以作为装饰器,但是我尝试了一下 python 其它数据结构, 都好像不支持, 想知道@到底是什么含义呢?

    11 replies    2021-02-01 23:54:20 +08:00
    AoEiuV020
        1
    AoEiuV020  
       Feb 1, 2021
    np 是什么,
    just1
        2
    just1  
       Feb 1, 2021
    numpy,矩阵乘法
    anzu
        3
    anzu  
       Feb 1, 2021
    SlipStupig
        4
    SlipStupig  
    OP
       Feb 1, 2021
    @anzu python3.5 以后提供矩阵计算标准库了嘛?
    Yienschwen
        5
    Yienschwen  
       Feb 1, 2021   ❤️ 1
    3.5 引入的一个新运算符 __matmul__, https://www.python.org/dev/peps/pep-0465/ https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types
    支不支持取决于这个类型有没有实现这运算符,正如其名它是用来进行矩阵乘法的,一般的数据结构不支持也是自然的
    Ediacaran
        6
    Ediacaran  
       Feb 1, 2021
    __matmul__ 方法
    nonoda
        7
    nonoda  
       Feb 1, 2021
    @AoEiuV020 import numpy as np
    nonduality
        8
    nonduality  
       Feb 1, 2021
    a1 @ b1 等价于 a1.dot(b1),其实我认为这是一个毫无价值、根本没必要引入的新语法。
    rogwan
        9
    rogwan  
       Feb 1, 2021
    @nonduality 增加一个价值不大的运算符,副作用更多,觉得 PEP572 引入的符号也没什么必要
    chizuo
        10
    chizuo  
       Feb 1, 2021
    @nonduality 在某些情况下还是有不一样的,你可以参见 numpy manual
    hsfzxjy
        11
    hsfzxjy  
       Feb 1, 2021 via Android
    作为中缀运算符看起来还是比.dot 清晰的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3890 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 05:07 · PVG 13:07 · LAX 22:07 · JFK 01:07
    ♥ Do have faith in what you're doing.