【发布时间】:2021-06-30 11:07:48
【问题描述】:
对象是否有它的方法或从 python 中的超类调用它们? 例如:
class tst():
def __init__(self,name,family):
self.name=name
self.family=family
def fun(self,a,b):
print(a+b)
newtst=tst("myname","my family")
tst.fun(newtst,3,5)
newtst.fun(3,5)
在上面的代码中,newtst 对象是否从tst 类调用fun 函数,或者它有自己的方法并直接运行它
如果后者是真的,为什么我们需要在定义类的函数中自参数
并且知道id(newtst.fun) 与id(tst.fun) 不同,但我认为差异是因为在内存中创建了新方法的对象。
【问题讨论】:
-
newtst是tst的一个实例。而且您不必将实例对象作为第一个参数传递给fun方法。 -
不,这是真的。自己试试