English 中文(简体)
洗 发电机
原标题:Python maneuvering with nested generators

我有这样的守则:

class A:
  def __iter__(self):
    for i in range(100):
      yield i

class B:
  def __init__(self, src):
    self.src = src
  def __iter__(self):
    for i in self.src:
      yield i * 2

class C:
  def __init__(self, src1, src2):
    self.src1 = src1
    self.src2 = src2
  def __iter__(self):
    for i, j in zip(self.src1, self.src2):
      yield i + j

a = A()
b = B(a)
c = C(a, b)
it = iter(c)
next(it)

我想做的是,在援引<代码>next(it)时,对电话链进行校正。 更具体地说,我想在每一类别中在<代码>yield后执行一些新的代码,而不必修改等级代码。 理想的是,实施<代码>yield的新代码打印。 这是可能的吗?

问题回答

You can create a subclass for each of the generator class with a wrapper __iter__ method that prints in which class yield is to be executed before yielding from the __iter__ method of the superclass. Since yield pauses the execution of the generator and returns control back to the caller, the wrapper should print such an output before, not after, yielding:

def print_yields(cls):
    class wrapper(cls):
        def __iter__(self):
            iterator = super().__iter__()
            for i in iterator:
                print(f yielding {i} from class {cls.__name__} with iterator {id(iterator):x} )
                yield i
    return wrapper

a = print_yields(A)()
b = print_yields(B)(a)
c = print_yields(C)(a, b)
for i in c:
    print(i)

Demo: https://replit.com/@blhsing/UprightWebbedArtificialintelligence





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签