220 字
1 分钟
Python “@” 符号
2023-11-08

Python “@” 符号#

1、 @设置函数属性值#

# 比如 @property可以使类方法转化成属性
class Student():
def __init__(self, name):
self._name = name
def sname(self):
return self._name
stu = Student('xiaoming')
print(stu.sname())
#xiaoming
class Student():
def __init__(self, name):
self._name = name
@property
def sname(self):
return self._name
stu = Student('xiaoming')
print(stu.sname)
#xiaoming

2、 @ 作为numpy的运算符(np.dot())#

from numpy import array,dot
a=array([[1,2],
[1,2]])
b=array([[5,6],
[5,6]])
print(a@b)
print(dot(a,b))
#[[15 18]
# [15 18]]
#[[15 18]
# [15 18]]

c = np.dot(a,v) # 向量点积或矩阵乘法,shape不合法时报错 或者 c=a.dot(b) d = np.multiply(a,v) # 先广播后对位相乘,shape无法广播时报错 e = a@v # 向量点积或矩阵乘法,shape不合法时报错 f = a*v # array:先广播后对位相乘;matrix:矩阵乘法,shape不合法时报错

Reference#

[1] python @修饰符用法 - 知乎

[2] python:@运算符(numpy库中的运算符)-CSDN博客

[3] Python NumPy中 dot() / multiply() / @ / * 之间的区别 - 知乎

Python “@” 符号
https://tuoyuangui.github.io/website/posts/typora/python/python-符号/
作者
椭圆规/tuoyuangui
发布于
2023-11-08
许可协议
CC BY-NC-SA 4.0