Python type of `def` -


this question has answer here:

python 3.6

can explain console output?

just started looking @ asyncio stuff , though i'd confirm how create coroutines , like. seems using def gives class function

>>> async def test(): ...     pass ...  >>> type(test) <class 'function'> # why not 'coroutine'?  >>> def test(): ...     yield ...      >>> type(test) <class 'function'>  >>> test = (i in range(0)) >>> type(test) <class 'generator'> # expected  >>> def test(): ...     in range(0): ...         yield ...          >>> type(test) <class 'function'> # why not 'generator'? 

you still have regular functions. these functions produce generator or coroutine, not generator or coroutine themselves.

compare generator function 1 returns generator expression:

def test():     return (i in range(0)) 

like function using yield, above function produces generator when call it; lets produce multiple, independent generators; generator expression on other hand can iterated on once.

the same applies coroutines. can produce function returns coroutine without using async:

def test():     return asyncio.sleep(0) 

the point of async function or generator function act factory; can repeatedly call them produce new coroutine or generator.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -