Python类中的自我
self 表示类的实例。通过使用“self”,我们可以访问Python中类的属性和方法。它将属性与给定的参数绑定。
你需要使用 self.是因为Python不使用 @ 语法来引用实例属性。 Python决定以一种使方法所属的实例自动传递但不自动接收的方式来执行方法:方法的第一个参数是调用该方法的实例。
更清楚地说,您可以说 SELF 具有以下特征-
Self is always pointing to Current Object.
Python3
#it is clearly seen that self and obj is referring to the same object
class check:
def __init__(self):
print("Address of self = ",id(self))
obj = check()
print("Address of class object = ",id(obj))
# this code is Contributed by Samyak Jain
Python3
# Write Python3 code here
class car():
# init method or constructor
def __init__(self, model, color):
self.model = model
self.color = color
def show(self):
print("Model is", self.model )
print("color is", self.color )
# both objects have different self which
# contain their attributes
audi = car("audi a4", "blue")
ferrari = car("ferrari 488", "green")
audi.show() # same output as car.show(audi)
ferrari.show() # same output as car.show(ferrari)
# Behind the scene, in every instance method
# call, python sends the instances also with
# that method call like car.show(audi)
Python3
# Self is always required as the first argument
class check:
def __init__():
print("This is Constructor")
object = check()
print("Worked fine")
# Following Error is produced if Self is not passed as an argument
Traceback (most recent call last):
File "/home/c736b5fad311dd1eb3cd2e280260e7dd.py", line 6, in
object = check()
TypeError: __init__() takes 0 positional arguments but 1 was given
# this code is Contributed by Samyak Jain
Python3
# Write Python3 code here
class this_is_class:
def __init__(in_place_of_self):
print("we have used another "
"parameter name in place of self")
object = this_is_class()
输出
Address of self = 140124194801032
Address of class object = 140124194801032
另一个使用 SELF 的例子:
Python3
# Write Python3 code here
class car():
# init method or constructor
def __init__(self, model, color):
self.model = model
self.color = color
def show(self):
print("Model is", self.model )
print("color is", self.color )
# both objects have different self which
# contain their attributes
audi = car("audi a4", "blue")
ferrari = car("ferrari 488", "green")
audi.show() # same output as car.show(audi)
ferrari.show() # same output as car.show(ferrari)
# Behind the scene, in every instance method
# call, python sends the instances also with
# that method call like car.show(audi)
输出
Model is audi a4
color is blue
Model is ferrari 488
color is green
Self is the first argument to be passed in Constructor and Instance Method.
Self 必须作为 First 参数提供给 Instance 方法和构造函数。如果您不提供,则会导致错误。
Python3
# Self is always required as the first argument
class check:
def __init__():
print("This is Constructor")
object = check()
print("Worked fine")
# Following Error is produced if Self is not passed as an argument
Traceback (most recent call last):
File "/home/c736b5fad311dd1eb3cd2e280260e7dd.py", line 6, in
object = check()
TypeError: __init__() takes 0 positional arguments but 1 was given
# this code is Contributed by Samyak Jain
Self is a convention and not a Python keyword .
self 是 Instance Method 中的参数,用户可以使用另一个参数名称来代替它。但建议使用self,因为它增加了代码的可读性,也是一种很好的编程习惯。
Python3
# Write Python3 code here
class this_is_class:
def __init__(in_place_of_self):
print("we have used another "
"parameter name in place of self")
object = this_is_class()
输出
we have used another parameter name in place of self