What is the difference between the self and instance keywords in Python 3?
I see code like,
def update(self, instance, validated_data):
Update and return an existing `Snippet` instance, given the validated data.
instance.title = validated_data.get('title', instance.title)
instance.code = validated_data.get('code', instance.code)
instance.linenos = validated_data.get('linenos', instance.linenos)
instance.language = validated_data.get('language', instance.language)
instance.style = validated_data.get('style', instance.style)
The question is rather generic, but let me see if I can shed some light on it for you:
self refers to the class(by convention, not a keyword) of which
update is a part. The class has variables and methods and you can refer to these with the
self keyword(not a reserved keyword) by calling
In the case of the snippet above,
self refers to the class.
instance likely refers to some
model instance "the big hint is the
validated_data is a dictionary or class object with attributes you are
gettting and assigning to
instance attributes before saving them
Hope this helps