[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: isgenerator(...) - anywhere to be found?

exarkun

1/22/2008 1:43:00 PM

On Tue, 22 Jan 2008 14:20:35 +0100, "Diez B. Roggisch" <deets@nospam.web.de> wrote:
>For a simple greenlet/tasklet/microthreading experiment I found myself in
>the need to ask the question
>
>isgenerator(v)
>
>but didn't find any implementation in the usual suspects - builtins or
>inspect.
>
>I was able to help myself out with a simple (out of my head, hope its
>
>def isgenerator(v):
> def _g(): yield
> return type(v) == type(_g())
>
>But I wonder why there is no such method already available?
>

Why do you need a special case for generators? If you just pass the
object in question to iter(), instead, then you'll either get back
something that you can iterate over, or you'll get an exception for
things that aren't iterable.

Jean-Paul
1 Answer

Diez B. Roggisch

1/22/2008 2:16:00 PM

0

Jean-Paul Calderone wrote:

> On Tue, 22 Jan 2008 14:20:35 +0100, "Diez B. Roggisch"
> <deets@nospam.web.de> wrote:
>>For a simple greenlet/tasklet/microthreading experiment I found myself in
>>the need to ask the question
>>
>>isgenerator(v)
>>
>>but didn't find any implementation in the usual suspects - builtins or
>>inspect.
>>
>>I was able to help myself out with a simple (out of my head, hope its
>>
>>def isgenerator(v):
>> def _g(): yield
>> return type(v) == type(_g())
>>
>>But I wonder why there is no such method already available?
>>
>
> Why do you need a special case for generators? If you just pass the
> object in question to iter(), instead, then you'll either get back
> something that you can iterate over, or you'll get an exception for
> things that aren't iterable.

Because - as I said - I'm working on a micro-thread thingy, where the
scheduler needs to push returned generators to a stack and execute them.
Using send(), which rules out iter() anyway.

Diez