Python 符号self(x)是做什么的?
我正在从“深度强化学习实践”学习分布式RL。模型类中有一个方法:Python 符号self(x)是做什么的?,python,deep-learning,reinforcement-learning,self,Python,Deep Learning,Reinforcement Learning,Self,我正在从“深度强化学习实践”学习分布式RL。模型类中有一个方法: def both(self, x): cat_out = self(x) probs = self.apply_softmax(cat_out) weights = probs * self.supports res = weights.sum(dim=2) return cat_out, res self(x)是什么意思?它将调用实例上的\
def both(self, x):
cat_out = self(x)
probs = self.apply_softmax(cat_out)
weights = probs * self.supports
res = weights.sum(dim=2)
return cat_out, res
self(x)是什么意思?它将调用实例上的
\uuuu call\uuu
方法。请参阅此演示:
class A:
def __call__(self, x):
print("called instance")
return x + 3
def both(self, x):
val = self(x)
print("value:", val)
a = A()
a.both(5)
print()
a(123)
a.__call__(123)
输出:
调用实例
数值:8
调用实例
调用实例
执行
val=self(x)
与执行val=self是相同的。这取决于类的\uu调用方法是如何定义的。在不知道的情况下,没有定义的含义。调用对象的方法。它相当于self.\uuuuu call\uuuuuuuuux)
。在提问之前,请确保您提供的代码足够完整,其他人可以看到相同的行为,而无需添加问题中不包含的任何内容(但不包含严格意义上不需要的内容)。任何(x)功能是什么?现在设置self=anything:在python中,一切都是一个对象。