Python中 @ 符号的作用
2025-12-15 本文已影响0人
王叽叽的小心情
遇到了一段Python代码,看到了@符号的形式,大致猜出来是矩阵相乘,果然是的,搜索得到@符号的两种功能,简单描述如下
1. 使用“@”操作符进行矩阵乘法
A @ B 的功能类似于np.dot(A,B)
使用@操作符进行矩阵乘法
只不过@方式是针对的普通的矩阵,np.dot()只能对于numpy的矩阵形式,找到一个更加详细的解释,@背后是调用了一个matmul()函数,来源https://blog.csdn.net/p15097962069/article/details/104005929,截图如下:
@操作符的功能
2. 装饰器的作用(decorator)
意思是你先定义了一个函数,之后想要在这个函数的基础上输出一些自定义的内容,这个时候就可以使用decorator,比如
def decorator(something):
return something
@decorator
def add_something():
return add-something
内容其实是继承了原来的decorator,然后作为参数传递给之前定义的函数,新增了一些内容,等价于
def decorator(something):
return something
def add_something():
return add_something
add_something = decorator(add_something)
别人的例子,用class的形式更直观
主要参考资料:https://blog.csdn.net/p15097962069/article/details/104005929