About 4,980,000 results
Open links in new tab
  1. Do union types actually exist in python? - Stack Overflow

    the type itself does not exist because Python is just a dynamically typed language, however, in newer Python versions, Union Type is an option for Type Hinting, from typing import Union,TypeVar T = TypeVar('T') def f(x: T) -> Union[str, None]: if x: return "x"

  2. python - mypy, type hint: Union[float, int] -> is there a Number …

    May 28, 2024 · For people who come to this question for the more general problem of Union typing hints for entities which don't have an existing supertype in common, for example Union[int, numpy.ndarray], the solution is to import Union from typing. Example 1: from typing import Union def my_func(number: Union[float, int]): # Do something Example 2:

  3. python - How should I use the Optional type hint? - Stack Overflow

    Python 3.10 introduces the | union operator into type hinting, see PEP 604. Instead of Union[str, int] you can write str | int. In line with other type-hinted languages, the preferred (and more concise) way to denote an optional argument in Python 3.10 and up, is now Type | None, e.g. str | None or list | None.

  4. What is the correct way in python to annotate a path with type …

    Oct 24, 2019 · I assume that typical path objects are either Paths or strs, as such you could use a Union. In addition, the more inclusive os.PathLike is preferred over pathlib.Path. Python 3.10 or newer: import os def read_json(path: str | os.PathLike): ... Python 3.6 - 3.9: import os import typing def read_json(path: typing.Union[str, os.PathLike]): ...

  5. python - Pythonic way to create union of all values contained in ...

    Since you seem to be using Python 2.5 (it would be nice to mention in your Q if you need an A for versions != 2.6, the current production one, by the way;-) and want a list rather than a set as the result, I recommend:

  6. dictionary - Union of dict objects in Python - Stack Overflow

    Mar 22, 2012 · Prior to Python 3.9, the simpler way to create a new dictionary is to create a new dictionary using the "star expansion" to add teh contents of each subctionary in place: c = {**a, **b} For dynamic dictionary combination, working as "view" to combined, live dicts:

  7. What does |= (ior) do in Python? - Stack Overflow

    Oct 14, 2010 · A thread on Python-ideas on why to use |= to update a set; A section B.8 of Dive in Python 3 on special methods of Python operators; In-place binary operators fallback to regular methods, see cpython source code (eval.c and abstract.c). Thanks @asottile. A post on how python handles displaying prepended zeros in bitwise computations

  8. Union of multiple sets in python - Stack Overflow

    Jun 11, 2015 · You could dramatically reduce the number of temporary sets by changing set.union(*map(set, a)) to set().union(*a). The only reason the map(set, was needed was because you were calling set.union and the first argument became the "self" it was being called on, but if you make an empty set as the base, union accepts arbitrary iterables for the ...

  9. python - Optional Union in type hint - Stack Overflow

    Then again, Union[None, int, str] takes less space than what would be more readable, namely Optional[Union[int, str]]. – GeeTransit Commented Mar 19, 2019 at 0:55

  10. python - Difference between union() and update() in sets, and …

    Python sets have these methods: s.union(t) s | t new set with elements from both s and t s.update(t) s |= t return set s with elements added from t Likewise, there's also these: s.intersection_update(t) s &= t return set s keeping only elements also found in t s.intersection(t) s & t new set with elements common to s and t