What does it mean by the 'super object returned is unbound' in python?

前端 未结 4 1318
轻奢々
轻奢々 2021-02-05 21:34

According to http://docs.python.org/2/library/functions.html#super,

If the second argument is omitted, the super object returned is unbound.

4条回答
  •  醉话见心
    2021-02-05 21:34

    To explain terms bound / unbound I will use functions, and — to be brief — I give up details and caveats.

    There are 2 views:


    1. From the user point of view there are 3 sorts of functions:
      1. Free function (unbound), for example the built-in function sum():

        sum([1, 2, 3])          # 6
        
      2. A function bound to an object (other name: object method) means that a user have to bind it to a particular object with the dot (.) notation.

        For example the use of built-in .upper() method:

        "Alice".upper()     # ALICE
        "Jacob".upper()     # JACOB (the same method; different object, different result)
        
      3. A function bound to a class (other name: class method) means that a user have to bind it to a particular class — again with the same dot (.) notation.

        Examples:

        A.some_class_method()               # A is a class
        B.some_class_method(parameters)     # B is a class
        

    1. From the designer point of view there are the same 3 sorts of functions, so I use the same numbers for them:

      1. A free (unbound) function is defined out of a class:

        def free_function(parameters):
            ...
        
      2. A function bound to an object is defined inside a class, with the first parameter reserved for an object and named self (only a convention, but a very strong one):

        class A:
            def bound_to_object(self, other_parameters):
                ...
        
      3. A function bound to a class is defined inside a class, with the first parameter reserved for a class and named cls (only a convention, but a very strong one) and with the @classmethod decorator just before it:

        class A:
            @classmethod
            def bound_to_class(cls, other_parameters):
                ...
        

提交回复
热议问题