Type for generator iterator?

327 views
Skip to first unread message

Damian Vuleta

unread,
Sep 10, 2015, 2:30:44 AM9/10/15
to cython-users
Another newbie question…

I have a Cython module that has two functions.

One is a generator with a Yield method (producing a list) within a 'While 1' loop, so it may be always callable.

The other is a loop that repeatedly calls on the generator. An iterator variable is created:

gen = generatorfunction(mem)
 # mem is the input, a memoryview

and successive results are called via 'next(gen)' as required.

Should the iterator variable 'gen' have a type? And what would it be? Or should I leave it as an uptyped Python object?

Stefan Behnel

unread,
Sep 10, 2015, 3:31:13 AM9/10/15
to cython...@googlegroups.com
Damian Vuleta schrieb am 10.09.2015 um 07:13:
> Another newbie question…
>
> I have a Cython module that has two functions.
>
> One is a generator with a Yield method (producing a list) within a 'While
> 1' loop, so it may be always callable.
>
> The other is a loop that repeatedly calls on the generator. An iterator
> variable is created:
>
> gen = generatorfunction(mem)
>
> *# mem is the input, a memoryview*
>
> and successive results are called via 'next(gen)' as required.
>
> Should the iterator variable 'gen' have a type? And what would it be? Or
> should I leave it as an uptyped Python object?

No need for typing here. Remember that Cython is an *optionally* typed
language. As long as there is no reason to declare the type of a variable
(such as your profiler hitting you on the fingers), just keep your code in
Python.

Stefan

Damian Vuleta

unread,
Sep 10, 2015, 4:57:41 AM9/10/15
to cython-users, stef...@behnel.de


On Thursday, 10 September 2015 17:31:13 UTC+10, Stefan Behnel wrote:
As long as there is no reason to declare the type of a variable
(such as your profiler hitting you on the fingers), just keep your code in
Python.

Stefan


That's why I asked. I'd paled or whitened most of the code when annotating, but that line remains a strong yellow. However, generators being a Python feature, I suspected that I might not be able to change that.

Robert Bradshaw

unread,
Sep 10, 2015, 5:10:57 AM9/10/15
to cython...@googlegroups.com
Every time you add a type annotation, you should ask yourself why
you're adding it. To make the annotated code whiter is not usually a
sufficient justification--it's primarily to catch surprises or help
you hunt down why code that a profiler implicated could be slow.

- Robert
Reply all
Reply to author
Forward
0 new messages