Yield (instruction)
- Yield (instruction)
-
Pour les articles homonymes, voir
Yield.
Le yield est une instruction de programmation qui sert à imposer la constructivité d'une fonction. Elle est plus souvent présente dans les langages à haut niveau comme Python, Ruby ou le C#. Son utilisation dans une fonction permet de retourner un générateur. Dans le cas du python, appeler la méthode next() de ce générateur exécutera la fonction et retournera une valeur. Le yield procède comme un return au détail près que cette fonction est liée a un générateur et que le prochain appel de la méthode next() reprendra l'exécution là où elle en était.
Exemple en language Python
def fonction(x):
print('début fonction')
while 1:
yield x
print('incrémente x')
x = x + 1
gen = fonction(2)
gen.next()
# début fonction
# 2
gen.next()
# incrémente x
# 3
Exemple en language Ruby
En Ruby, l'opérateur yield va entrainer l'exécution du bloc de code passé en paramètre :
def delimit
puts "before yield"
yield if block_given?
puts "after yield"
end
delimit{ puts "in yield" } #=> "before yield\n" "in yield\n" "after yield\n"
Lien externe
- (en) yield sur la documentation de python.org
Wikimedia Foundation.
2010.
Contenu soumis à la licence CC-BY-SA. Source : Article Yield (instruction) de Wikipédia en français (auteurs)
Regardez d'autres dictionnaires:
Yield — Cette page d’homonymie répertorie les différents sujets et articles partageant un même nom. Le yield est un anglicisme qui désigne : Un terme économique : yield management Une instruction en programmation : yield (instruction) Le… … Wikipédia en Français
Reduced instruction set computer — The acronym RISC (pronounced risk ), for reduced instruction set computing, represents a CPU design strategy emphasizing the insight that simplified instructions which do less may still provide for higher performance if this simplicity can be… … Wikipedia
dynamite instruction — Further instruction given by the trial judge to jury when the jury have reported an inability to agree on a verdict in a criminal case. In the further instructions, the judge advises them of their obligation to consider the opinions of their… … Black's law dictionary
dynamite instruction — Further instruction given by the trial judge to jury when the jury have reported an inability to agree on a verdict in a criminal case. In the further instructions, the judge advises them of their obligation to consider the opinions of their… … Black's law dictionary
μ operator — In computability theory, the μ operator, minimization operator, or unbounded search operator searches for the least natural number with a given property. Contents 1 Definition 2 Properties 3 Examples … Wikipedia
Motorola 6800 — Motorola MC6800 Microprocessor. The 6800 was an 8 bit microprocessor designed and first manufactured by Motorola in 1974. The MC6800 microprocessor was part of the M6800 Microcomputer System that also included serial and parallel interface ICs,… … Wikipedia
biblical literature — Introduction four bodies of written works: the Old Testament writings according to the Hebrew canon; intertestamental works, including the Old Testament Apocrypha; the New Testament writings; and the New Testament Apocrypha. The Old… … Universalium
Coroutine — Coroutines are computer program components that generalize subroutines to allow multiple entry points for suspending and resuming execution at certain locations. Coroutines are well suited for implementing more familiar program components such as … Wikipedia
Italy — • In ancient times Italy had several other names: it was called Saturnia, in honour of Saturn; Enotria, wine producing land; Ausonia, land of the Ausonians; Hesperia, land to the west (of Greece); Tyrrhenia, etc. The name Italy, which seems to… … Catholic encyclopedia
Russia — • Geography and history Catholic Encyclopedia. Kevin Knight. 2006. Russia Russia † Catholic E … Catholic encyclopedia