Python3:可以使用 'self' 以外的方法作为方法的第一个参数吗?
Python3: Can use other than 'self' as a 1st parameter of methods?
这个代码可以吗
class Point:
def __init__(self,x,y):
self.x=x
self.y=y
def __str__(self):
return "Point({},{})".format(self.x,self.y)
p=Point(3,5)
print(p)
修改为以下代码?
class Point:
def __init__(p,x,y):
p.x=x
p.y=y
def __str__(p):
return "Point({},{})".format(p.x, p.y)
p=Point(3,5)
print(p)
在这种情况下似乎可行。但它是如此天真。我想知道在某些情况下使用 'self' 以外的其他方式会导致一些问题。
第一个参数是对绑定变量或对象的引用。它的惯例是使用 self 但其他任何东西也都可以。检查文档 here.
这个代码可以吗
class Point:
def __init__(self,x,y):
self.x=x
self.y=y
def __str__(self):
return "Point({},{})".format(self.x,self.y)
p=Point(3,5)
print(p)
修改为以下代码?
class Point:
def __init__(p,x,y):
p.x=x
p.y=y
def __str__(p):
return "Point({},{})".format(p.x, p.y)
p=Point(3,5)
print(p)
在这种情况下似乎可行。但它是如此天真。我想知道在某些情况下使用 'self' 以外的其他方式会导致一些问题。
第一个参数是对绑定变量或对象的引用。它的惯例是使用 self 但其他任何东西也都可以。检查文档 here.