| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Documentation build overview1 file changed ± pep-0824/index.html |
Sorry, something went wrong.
There was a problem hiding this comment.
Thanks @gvanrossum. Chaining and operator precedence sections are very helpful and descriptive. Leaving for the PEP author to weigh in prior to merging.
Sorry, something went wrong.
There was a problem hiding this comment.
Similar to the other PR, it could be nice to add a short note to the (to be created) Change History section.
Sorry, something went wrong.
| For example, ``d.get(key) ?? make_default(key)`` is roughly equivalent to | ||
| ``d.get(key, make_default(key))``, with two differences: | ||
|
|
||
| * ``??`` only evaluates ``make_default(key)`` when needed, whereas the | ||
| function call used as the default argument to ``dict.get`` is always | ||
| evaluated. | ||
| * ``??`` also returns the default when ``d[key]`` is ``None``, whereas | ||
| ``dict.get`` returns ``None`` in that case. |
There was a problem hiding this comment.
I needed to read this section multiple times to fully understand it. While correct, I don't know if it adds much value here. I'd just remove it.
Sorry, something went wrong.
| Adding parentheses does not change this: ``(a ?? b) ?? c`` has the same | ||
| meaning as ``a ?? (b ?? c)``, since ``??`` is an associative operator. |
There was a problem hiding this comment.
Can we mention that or is also an associative operator and the behavior is similar to that?
Sorry, something went wrong.
| When to use | ||
| ----------- | ||
|
|
||
| This is useful when you find yourself writing expressions like | ||
| ``a if a is not None else b`` a lot. The ``??=`` operator is handy for | ||
| providing defaults for arguments. For example: |
There was a problem hiding this comment.
I think it would make sense if we included an example for just ?? here as well. Maybe something along these lines? Cases where or is used just to filter out None.
def get_user_names(users: list[User] | None = None) -> list[str]:
return [user.name for user in users or ()] # `or` -> `??`
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Don't merge until @cdce8p (Marc Mueller, the PEP author) approves!