Update the Dictionary.get regarding the default argument & it's side effects
Co-authored-by: Danil Alexeev <dalexeev12@yandex.ru>
This commit is contained in:
parent
833889aec0
commit
0ced9ff855
1 changed files with 7 additions and 0 deletions
|
|
@ -256,6 +256,13 @@
|
|||
<param index="1" name="default" type="Variant" default="null" />
|
||||
<description>
|
||||
Returns the corresponding value for the given [param key] in the dictionary. If the [param key] does not exist, returns [param default], or [code]null[/code] if the parameter is omitted.
|
||||
[b]Note:[/b] If the [param default] argument is computationally expensive or has unwanted side effects, consider using the [method has] method instead:
|
||||
[codeblock]
|
||||
# Always calls `expensive_function()`.
|
||||
dict.get("key", expensive_function())
|
||||
# Calls `expensive_function()` only if the key does not exist.
|
||||
dict.get("key") if dict.has("key") else expensive_function()
|
||||
[/codeblock]
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_or_add">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue