Skip to content

内置函数

abs()



Result 1



Result 2



Result 3


all()



Result 1



Result 2



Result 3


any()



Result 1



Result 2



Result 3


ascii()



Result 1



Result 2



Result 3


bin()



Result 1



Result 2



Result 3


bool()



Result 1



Result 2



Result 3


bytearray()



Result 1



Result 2



Result 3


bytes()



Result 1



Result 2



Result 3


callable()



Result 1



Result 2



Result 3


chr()



Result 1



Result 2



Result 3


classmethod()



Result 1



Result 2



Result 3


compile()



Result 1



Result 2



Result 3


complex()



Result 1



Result 2



Result 3


delattr()



Result 1



Result 2



Result 3


dict()



Result 1



Result 2



Result 3


dir()



Result 1



Result 2



Result 3


divmod()



Result 1



Result 2



Result 3


enumerate()



Result 1



Result 2



Result 3


eval()



Result 1



Result 2



Result 3


exec()



Result 1



Result 2



Result 3


filter()



Result 1



Result 2



Result 3


float()



Result 1



Result 2



Result 3


format()



Result 1



Result 2



Result 3


frozenset()



Result 1



Result 2



Result 3


getattr()



Result 1



Result 2



Result 3


globals()



Result 1



Result 2



Result 3


hasattr()



Result 1



Result 2



Result 3


hash()



Result 1



Result 2



Result 3


help()



Result 1



Result 2



Result 3


hex()



Result 1



Result 2



Result 3


id()



Result 1



Result 2



Result 3


input()



Result 1



Result 2



Result 3


int()



Result 1



Result 2



Result 3


isinstance()



Result 1



Result 2



Result 3


issubclass()



Result 1



Result 2



Result 3


iter()



Result 1



Result 2



Result 3


len()



Result 1



Result 2



Result 3


list()



Result 1



Result 2



Result 3


locals()



Result 1



Result 2



Result 3


map()



Result 1



Result 2



Result 3


max()



Result 1



Result 2



Result 3


memoryview()



Result 1



Result 2



Result 3


min()



Result 1



Result 2



Result 3


next()



Result 1



Result 2



Result 3


object()



Result 1



Result 2



Result 3


oct()



Result 1



Result 2



Result 3


open()



Result 1



Result 2



Result 3


ord()



Result 1



Result 2



Result 3


pow()



Result 1



Result 2



Result 3


print()



Result 1



Result 2



Result 3


property()



Result 1



Result 2



Result 3


range()



Result 1



Result 2



Result 3


repr()



Result 1



Result 2



Result 3


reversed()



Result 1



Result 2



Result 3


round()



Result 1



Result 2



Result 3


set()



Result 1



Result 2



Result 3


setattr()



Result 1



Result 2



Result 3


slice()



Result 1



Result 2



Result 3


sorted()



Result 1



Result 2



Result 3


staticmethod()



Result 1



Result 2



Result 3


str()



Result 1



Result 2



Result 3


sum()



Result 1



Result 2



Result 3


super()



Result 1



Result 2



Result 3


tuple()



Result 1



Result 2



Result 3


type()



Result 1



Result 2



Result 3


vars()



Result 1



Result 2



Result 3


zip()



Result 1



Result 2



Result 3


__import__()



Result 1



Result 2



Result 3


super()

super() 内建方法,会返回一个代理对象(superclass 的临时对象),它允许我们访问基类(base class)的方法

在 Python 中,super() 主要有两种用途:

  • 允许我们避免显式使用基类名称
  • 使用多重继承
super().method(*args, **kwargs)其中method()  base class 中的方法
# 示例
class Animal(object):
    def __init__(self, animal_type):
        print('Animal Type:', animal_type)

class Mammal(Animal):
    def __init__(self):

        # call superclass
        super().__init__('Mammal')
        print('Mammals give birth directly')

dog = Mammal()

# Output: Animal Type: Mammal
#         Mammals give birth directly

pass

pass

super() with Single Inheritance: 使用 super() 来 refer base class

class Mammal(object):
    def __init__(self, mammalName):
        print(mammalName, 'is a warm-blooded animal.')

class Dog(Mammal):
    def __init__(self):
        print('Dog has four legs.')
        super().__init__('Dog')

d1 = Dog()

Result 1

Dog has four legs.
Dog is a warm-blooded animal.

此处,是从 Dog 类中,直接调用了 Mammal 类的 __init__ 方法,即 super().__init__('Dog'),而非是 Mammal.__init__(self, 'Dog')

由于我们在调用其成员时不需要指定基类的名称,因此如果需要的话,我们可以轻松更改基类名称。

# changing base class to CanidaeFamily
class Dog(CanidaeFamily):
    def __init__(self):
        print('Dog has four legs.')

        # no need to change this
        super().__init__('Dog')

The super() builtin returns a proxy object, a substitute object that can call methods of the base class via delegation. This is called indirection (ability to reference base object with super())

Since the indirection is computed at the runtime, we can use different base classes at different times (if we need to).

super() with Multiple Inheritance

class Animal:
    def __init__(self, Animal):
        print(Animal, 'is an animal.');

class Mammal(Animal):
    def __init__(self, mammalName):
        print(mammalName, 'is a warm-blooded animal.')
        super().__init__(mammalName)

class NonWingedMammal(Mammal):
    def __init__(self, NonWingedMammal):
        print(NonWingedMammal, "can't fly.")
        super().__init__(NonWingedMammal)

class NonMarineMammal(Mammal):
    def __init__(self, NonMarineMammal):
        print(NonMarineMammal, "can't swim.")
        super().__init__(NonMarineMammal)

class Dog(NonMarineMammal, NonWingedMammal):
    def __init__(self):
        print('Dog has 4 legs.');
        super().__init__('Dog')

d = Dog()
print('')
bat = NonMarineMammal('Bat')        

Result 2

Dog has 4 legs.
Dog can't swim.
Dog can't fly.
Dog is a warm-blooded animal.
Dog is an animal.

Bat can't swim.
Bat is a warm-blooded animal.
Bat is an animal.

Method Resolution Order (MRO)

Method Resolution Order (MRO) 是指在存在多重继承的情况下,应该继承方法的顺序。可以使用 __mro__ 属性查看 MRO。

MRO如何运作:

  • 派生调用中的方法总是在基类的方法之前调用。即 Dog 类在 NonMarineMammal 或 NoneWingedMammal 之前被调用。这两个类在 Mammal 之前被调用,Mammal 类在 Aninal 类之前被调用。而 Aninal 类则在 object 之前被调用;
  • 如果有多个像 Dog(NonMarineMammal, NonWingedMammal) 这样的 parents,则首先调用 NonMarineMammal 的方法,因为它首先出现。
>>> Dog.__mro__

Result 3

(<class 'Dog'>, 
<class 'NonMarineMammal'>, 
<class 'NonWingedMammal'>, 
<class 'Mammal'>, 
<class 'Animal'>, 
<class 'object'>)

拓展阅读