简体   繁体   English

Python 的自我与实例

[英]Python's self vs instance

What is the difference between the self and instance keywords in Python 3? Python 3 中的 self 和 instance 关键字有什么区别?

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)
    instance.save()
    return instance

The snippet is a bit short but instance is not a keyword (neither self , that is just convention).代码片段有点短,但instance不是关键字(都不是self ,这只是约定)。

It is an argument to another instance of another (maybe same) class.它是另一个(可能相同)类的另一个实例的参数。

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. self指的是update是其中一部分的类(按照惯例,不是关键字)。 The class has variables and methods and you can refer to these with the self keyword(not a reserved keyword) by calling self.update(instance, validated_data)该类具有变量和方法,您可以通过调用self.update(instance, validated_data)使用self关键字(不是保留关键字)引用这些变量和方法

In the case of the snippet above, self refers to the class.在上面的代码片段中, self指的是类。 instance likely refers to some model instance "the big hint is the instance.save() and validated_data is a dictionary or class object with attributes you are get tting and assigning to instance attributes before saving them instance可能指的是某个model实例“最大的提示是instance.save()validated_data是一个字典或类对象,具有您在保存之前get并分配给instance属性的属性

Hope this helps希望这可以帮助

Neither self nor instance are keywords in Python. selfinstance都不是 Python 中的关键字。 The identifier self is used by convention as the first parameter of instance methods in a class.标识符self按照惯例用作类中实例方法的第一个参数。 The object instance on which a method is called is automatically passed in as the first parameter.调用方法的对象实例会自动作为第一个参数传入。

In the above snippet, update is most probably a method of some class and self seems to be the conventional first parameter as described above.在上面的代码片段中, update很可能是某个类的方法,而self似乎是如上所述的常规第一个参数。 The second parameter instance is just another parameter and the name instance does not have any significance in Python.第二个参数instance只是另一个参数,名称instance在 Python 中没有任何意义。

You are refering to the implementation of a Serializer class in Django REST framework, that serializes data into Json with help of a Model object.您指的是 Django REST framework 中 Serializer 类的实现,该类在 Model 对象的帮助下将数据序列化为 Json。 This class has a method called 'update', which takes the Snippet Model object 'instance' as an input, and then updates this object with the validated_data.这个类有一个叫做“update”的方法,它以Snippet Model对象“instance”作为输入,然后用validated_data更新这个对象。 Like it was mentioned before 'instance' is not a reserved keyword in Python.就像之前提到的那样,'instance' 不是 Python 中的保留关键字。

A serializer class is very similar to a Django Form class, and includes similar validation flags on the various fields, such as required, max_length and default.序列化器类与 Django 表单类非常相似,并且在各个字段上包含类似的验证标志,例如 required、max_length 和 default。

In order get clearer idea I am sharing the complete class implementation.为了获得更清晰的想法,我分享了完整的类实现。

class SnippetSerializer(serializers.Serializer):

    id = serializers.IntegerField(read_only=True)
    title = serializers.CharField(required=False, allow_blank=True, max_length=100)
    code = serializers.CharField(style={'base_template': 'textarea.html'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')

    def create(self, validated_data):
        """
        Create and return a new `Snippet` instance, given the validated data.
        """
        return Snippet.objects.create(**validated_data)

    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)
        instance.save()
        return instance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM