python支援函式過載嗎 Python函式過載

2021-10-16 21:33:54 字數 2998 閱讀 1151

您所要求的是稱為多重排程。請參閱演示不同型別分派的julia語言示例。

然而,在討論這個問題之前,我們將首先討論為什麼在python中過載並不是您真正想要的。

為什麼不超載呢?

首先,需要理解過載的概念以及為什麼它不適用於python。when working with languages that can discriminate data types at

compile-time, selecting among the alternatives can occur at

compile-time. the act of creating such alternative functions for

compile-time selection is usually referred to as overloading a

function. (wikipedia)

python是一種dynamically型別語言,因此過載的概念並不適用於它。但是,並不是所有的都丟失了,因為我們可以在執行時建立這樣的替代函式:in programming languages that defer data type identification until

run-time the selection among alternative

functions must occur at run-time, based on the dynamically determined

types of function arguments. functions whose alternative

implementations are selected in this manner are referred to most

generally as multimethods. (wikipedia)

因此,我們應該能夠在python中執行多方法,或者,也就是我們所說的:多分派。

多次排程

多方法也稱為多分派:multiple dispatch or multimethods is the feature of some

object-oriented programming languages in which a function or method

can be dynamically dispatched based on the run time (dynamic) type of

more than one of its arguments. (wikipedia)

python不支援這種現成的1,但是,碰巧有乙個名為multipledispatch的優秀python包可以做到這一點。

解決方案

下面是我們如何使用multipledispatch2包來實現您的方法:>>> from multipledispatch import dispatch

>>> from collections import namedtuple

>>> from types import * # we can test for lambda type, e.g.:

>>> type(lambda a: 1) == lambdatype

true

>>> sprite = namedtuple('sprite', ['name'])

>>> point = namedtuple('point', ['x', 'y'])

>>> curve = namedtuple('curve', ['x', 'y', 'z'])

>>> vector = namedtuple('vector', ['x','y','z'])

>>> @dispatch(sprite, point, vector, int)

... def add_bullet(sprite, start, direction, speed):

... print("called version 1")

>>> @dispatch(sprite, point, point, int, float)

... def add_bullet(sprite, start, headto, speed, acceleration):

... print("called version 2")

>>> @dispatch(sprite, lambdatype)

... def add_bullet(sprite, script):

... print("called version 3")

>>> @dispatch(sprite, curve, int)

... def add_bullet(sprite, curve, speed):

... print("called version 4")

>>> sprite = sprite('turtle')

>>> start = point(1,2)

>>> direction = vector(1,1,1)

>>> speed = 100 #km/h

>>> acceleration = 5.0 #m/s

>>> script = lambda sprite: sprite.x * 2

>>> curve = curve(3, 1, 4)

>>> headto = point(100, 100) # somewhere far away

>>> add_bullet(sprite, start, direction, speed)

called version 1

>>> add_bullet(sprite, start, headto, speed, acceleration)

called version 2

>>> add_bullet(sprite, script)

called version 3

>>> add_bullet(sprite, curve, speed)

called version 4

2。注意不要在多執行緒環境中使用multipledispatch,否則會出現奇怪的行為。

python支援過載嗎 python有過載嗎

在python中,具有過載的思想卻沒有過載的概念。所以有的人說python這麼語言並不支援函式過載,有的人說python具有過載功能。實際上python程式設計中具有過載的目的缺無過載的行為,或者說是python並不需要過載!python是一門動態語言,不需要宣告變數型別,函式中可以接受任何型別的引...

Python為什麼不支援函式過載?

在考慮為什麼python不提供函式過載前,我們要研究為什麼需要提供函式過載。在靜態語言中,方法過載是希望類可以以統一的方式處理不同型別的資料提供可能性。多個同名函式同時存在 函式過載主要解決兩個問題 另外,乙個基本的設計原則是,僅僅當兩個函式除了引數型別和引數個數不同以外,其功能是完全相同的,此時才...

C 支援函式過載,C語言不支援函式過載的原因

c 支援函式過載,c語言不支援函式過載的原因?函式過載 在同一作用域內,一組具有不同引數列表的同名函式。通常情況,這組函式具有相似的功能,函式過載有效解決了只由於引數型別不同而造成的函式名數量膨脹問題。而c 支援函式過載,c不支援函式過載的原因是 c 在編譯過程中對函式的重新命名規則是 函式名 引數...