0

In python, there are some built-in classes that are not normally accessible such as Code, NoneType, and Generator. This question asks about some of these, and the answer essentially seems to be "don't worry about it." However, I specifically want to check if an object passed to my function is a generator function. Currently the relevant structure of my code is as follows.

def dummyGen_():
    yield None

Generator = type(dummyGen_())

def myFunct(gen):
   g = gen()
   assert type(g) is Generator
   # rest of the code #

However defining a useless generator that doesn't do anything just to get to its type feels awkward, and I just don't like it. Is there a better, more pythonic way of going about this? Or am I doomed to just create a random generator just to access it's type?

tox123
  • 318
  • 9
  • 21
  • does this help? https://docs.python.org/3/library/typing.html#typing.Generator – acushner Jun 11 '21 at 18:07
  • 3
    Insisting that your parameter be a generator function is inherently unpythonic. *Any* iterator fulfills the same interface as a generator, so you're just needlessly restricting the uses of your function. – jasonharper Jun 11 '21 at 18:08
  • @jasonharper that's a good point, and I feel a little silly now, because I entirely forgot iterators are a thing. The only objection I would have, is that I actually need the iterators to never halt, but I suppose that they would throw an objection anyways if they did halt. – tox123 Jun 11 '21 at 19:29

0 Answers0