Tuple from iterable #4128
Unanswered
kevinhartman
asked this question in
Questions
Replies: 3 comments 2 replies
-
You can call |
Beta Was this translation helpful? Give feedback.
2 replies
-
CPython has a special |
Beta Was this translation helpful? Give feedback.
0 replies
-
Another option: just call In [1]: def iterable():
...: yield 1
...: yield 2
...:
In [2]: tuple(iterable())
Out[2]: (1, 2) Thus #[pyfunction]
fn test_tuple(py: Python<'_>, arg: Bound<'_, PyAny>) -> PyResult<PyObject> {
let builtins = PyModule::import(py, "builtins")?;
let rv = builtins.call_method1("tuple", (arg.as_ref(),))?;
Ok(rv.unbind())
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I frequently find myself writing code like this to efficiently build a tuple from an iterable:
Is there a better way to do this? As I understand it, downcasting should be faster if
seq
is actually already a tuple (callingtuple()
on something in CPython is a no-op I believe if the thing is already a tuple, and I'd like those semantics here.(FWIW, I realize this code could probably be even faster if I were to skip the
is_instance_of
check and instead handle thePyResult
returned fromdowncast_exact
.)Beta Was this translation helpful? Give feedback.
All reactions