Why does Python seem to treat instance variables as shared between objects?

前端 未结 3 682
旧巷少年郎
旧巷少年郎 2020-12-17 02:12

I was working on a simple script today when I noticed a strange quirk in the way Python treats instance variables.

Say we have a simple object:

class         


        
3条回答
  •  时光取名叫无心
    2020-12-17 03:04

    Unlike in a compiled language, the class statement in Python is actually code that executes, and creates a class object (not an instance!) in memory.

    Any symbols that are defined when the class block runs belong to the class itself. This includes variables, such as the eggs variable in your first example, as well as the __init__ and __str__ methods that you define. All of those are created when the class is defined, and they are all part of the class.

    Instance variables are not created until you actually create an instance of the object, and the __init__ method is run, and they have to be attributes of self.

    So, when the python interpreter executes

    class Spam(object):
        eggs = {}
    
        def __init__(self):
            
        def __str__(self):
            
    

    it is actually building a class object at run time. It executes the code "eggs={}", and it executes the two def statements, and it builds a class that has three attributes: eggs, __init__ and __str__.

    Later, when it executes

    spam1 = Spam()
    

    Then it creates a new instance, and runs its __init__ method. The __init__ method itself, of course, belongs to the class; it is shared between all instances, just like the eggs attribute.

    The instance itself gets passed in as the self parameter, and anything you define on it belong to that instance alone. That's why self has to be passed into every class method -- in python, the methods actually belong to the class itself, and self is the only way that you have to refer to the instance.

提交回复
热议问题