Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7706065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:01:23+00:00 2026-06-01T00:01:23+00:00

I’m new to Python’s iterators, so maybe my language is not always correct. I

  • 0

I’m new to Python’s iterators, so maybe my language is not always correct.

I have a class wrapping a list of numpy.ndarray:

class wrapper:
    def __init__(self, myList):
        self.myList = myList

    def getArrayIterator(self):
        for arr in self.myList:
            yield arr

#set list of arrays in wrapper
myList = [rand(3,3), rand(3,3), rand(3,3)]
w = wrapper(myList)

As I understood it, the second method returns a generator.
Now I want to use that generator to loop over the list and reset the arrays to something else:

for a in w.getArrayIterator():
    a = zeros((3,4))

I was hoping to have pass by reference semantics here, but that doesn’t seem to be the case.

So I tried using Python’s send() in my getArrayIterator function:

# ...
def getArrayIterator(self):
    for arr in self.myList:
        val = (yield arr)
        if val is not None:
            arr = val
# ...

But that wont work either because:

a.send(zeros((3,4)))
  AttributeError: 'numpy.ndarray' object has no attribute 'send'

Is there a simple solution to achieve my desired behavior?
Am I missing something?


EDIT: It has been pointed out to me, that I should provide more Information about my actual problem. The example above is of course simplified.

I have a list of lists of numpy.ndarray representing a tensor T encapsulated in my class. When accessing an element of T: t_ijkl I need to multiply the matrices stored in the list: A(i)*B(j)*C(k)*D(l), first and last are row / col vectors.

So there is a set of A, a set of B, etc. Each of which belongs to a gridpoint in my application.

I now wanted to have an iterator over all gridpoints and iterators over the matrices associated with each gridpoint.

The first Idea that came to mind was to use iterators c++ style to read and write the matrices. But as agf pointed out below, this is not really a feasible approach. So I think I’ll use the different iterators just for read access and specialized setter methods to set new values for the matrices.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:01:24+00:00Added an answer on June 1, 2026 at 12:01 am

    I’m assuming this is a simplified example, because with the class you have there seems to be no reason not to just use a normal list.

    When you do

    name = somelist[0]
    name = 'other'
    

    you’re pointing the name name at the object at the first index in somelist, then pointing the name name at the object 'other'. You’re not ever pointing somelist[0] at 'other'.

    So in addition to send, you need to actually assign to the list:

    class wrapper:
        def __init__(self, myList):
            self.myList = myList
    
        def getArrayIterator(self):
            for i, arr in enumerate(self.myList):
                yarr = (yield arr)
                if yarr is not None:
                    self.myList[i] = yarr
    
    #set list of arrays in wrapper
    myList = [1, 2, 3, 4, 5]
    w = wrapper(myList)
    witer = w.getArrayIterator()
    try:
        a = next(witer)
        for i in range(6, 11):
            print a,
            a = witer.send(i)
    except StopIteration:
        pass
    print
    print w.myList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.