Python Forum
detecting a generstor passed to a funtion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
detecting a generstor passed to a funtion
#3
from collections.abc import Generator
from typing import Generator as TypingGenerator
from inspect import isgeneratorfunction, isgenerator


def foo():
    """Function"""

def bar():
    """Generatorfunction"""
    yield


print("foo", isgeneratorfunction(foo))
print("bar", isgeneratorfunction(bar))

print("foo()", isgenerator(foo()))
print("bar()", isgenerator(bar()))

# Generator could not detect a Generatorfunction
# but a Generator

print("bar()", isinstance(bar(), Generator))
print("bar()", isinstance(bar(), TypingGenerator))
I'm not sure about typing.Generator. It detects a generator, but typing is used for type hints, which is used for IDEs and Code Linters. One question, many solutions...
buran likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: detecting a generstor passed to a funtion - by DeaD_EyE - Sep-19-2021, 06:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 345 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  iterating and detecting the last Skaperen 3 1,079 Oct-01-2022, 05:23 AM
Last Post: Gribouillis
  Detecting float or int in a string Clunk_Head 15 4,538 May-26-2022, 11:39 PM
Last Post: Pedroski55
  module detecting if imported vs not Skaperen 1 1,675 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  Python BLE Scanner not detecting device alexanderDennisEnviro500 0 2,010 Aug-01-2021, 02:29 AM
Last Post: alexanderDennisEnviro500
  how to modify a variable that is passed as parameter StefanL38 2 2,118 Dec-07-2020, 08:39 AM
Last Post: StefanL38
  Detecting power plug Narayan 2 2,717 Aug-01-2020, 04:29 AM
Last Post: bowlofred
  Detecting USB Device Insertion on Windows 10 Atalanttore 0 2,383 Jan-17-2020, 02:46 PM
Last Post: Atalanttore
  are numeric types passed by value or reference? rudihammad 4 2,623 Nov-19-2019, 06:25 AM
Last Post: rudihammad
  Detecting windows shutdown event riccardoob 4 5,713 Nov-12-2019, 04:51 PM
Last Post: Aurthor_King_of_the_Brittons

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020