Is there a way to subclass a generator in Python 3?

2024/11/16 16:30:09

Aside from the obvious, I thought I'd try this, just in case:

def somegen(input=None):...yield...gentype = type(somegen())
class subgen(gentype):def best_function_ever():...

Alas, Python's response was quite hostile:

"TypeError: Type generator is not an acceptable base type"

As luck would have it, that's a problem for me. See, I was thinking that maybe it would be a fun base type to play with, if I gave it a chance. Imagine my surprise! ..and dismay. Is there no way to get the almighty Python to see things my way on this one?

This is most certainly an outside-the-box kinda question, so please don't just say that it's not possible if you can't think of a way immediately. Python (especially Py3) is very flexible.

Of course, if you have evidence of why it cannot (not "should not") be a base type (Py3), then I do want to see and understand that.

Answer

A relevant other question is Which classes cannot be subclassed?.

It's reason 2 in the accepted answer there -- subclassing of a type needs to be implemented in C, and it wasn't implemented for generators, probably because nobody saw a use case.

The source code for generator objects is genobject.c, and you can see on line 349 that the Py_TPFLAGS_BASETYPE flag is not set.

https://en.xdnf.cn/q/71651.html

Related Q&A

represent binary search trees in python

how do i represent binary search trees in python?

Python os.path.commonprefix - is there a path oriented function?

So I have this python code:print os.path.commonprefix([rC:\root\dir,rC:\root\dir1])Real ResultC:\root\dirDesired resultC:\rootQuestion 1Based on os.path.commonprefix documentation: Return the longest p…

Importing Stripe into Django - NameError

I cant seem to figure out how to import Stripe into my Django project. Im running Python 2.7.3 and I keep receiving NameError at /complete/ global name. stripe is not defined.Even when I just open up T…

getting line-numbers that were changed

Given two text files A,B, what is an easy way to get the line numbers of lines in B not present in A? I see theres difflib, but dont see an interface for retrieving line numbers

How to subclass a subclass of numpy.ndarray

Im struggling to subclass my own subclass of numpy.ndarray. I dont really understand what the problem is and would like someone to explain what goes wrong in the following cases and how to do what Im t…

How to ignore an invalid SSL certificate with requests_html?

So basically Im trying to scrap the javascript generated data from a website. To do this, Im using the Python library requests_html. Here is my code :from requests_html import HTMLSession session = HTM…

Fabric asks for root password

I am using Fabric to run the following:def staging():""" use staging environment on remote host"""env.user = ubuntuenv.environment = stagingenv.hosts = [host.dev]_setup_pa…

Beautifulsoup results to pandas dataframe

The below code returns me a table with the following resultsr = requests.get(url) soup = bs4.BeautifulSoup(r.text, lxml)mylist = soup.find(attrs={class: table_grey_border}) print(mylist)results - it st…

XGBoost CV and best iteration

I am using XGBoost cv to find the optimal number of rounds for my model. I would be very grateful if someone could confirm (or refute), the optimal number of rounds is: estop = 40res = xgb.cv(params, d…

Whats the correct way to implement a metaclass with a different signature than `type`?

Say I want to implement a metaclass that should serve as a class factory. But unlike the type constructor, which takes 3 arguments, my metaclass should be callable without any arguments:Cls1 = MyMeta()…