subject
Computers and Technology, 10.02.2020 22:19 riah133

The Backwardable class decorates iterables (and is itself iterable), allowing calls to both the next and prev functions: next is standard and defined in builtins; I’ve defined a similar prev function in q4solution. py (to call the __prev__ method defined in Backwardable’s B_iter class). So, we can move both forward and backward in the iterable that Backwardable class decorates by having it remember a list of previous values. In addition the clear function can be called to forget permanently earlier values that are unneeded (saving space). Use only classes, not generators. For example, given i = iter(Backwardable(’abc’)) then we could call both next(i) and prev(i) to iterate over the string. The sequence of calls on the left would produce the values on the right (the far-right is print(i)). See a longer example (also using clear) in the q4solution. py file. Executes Prints What print(i) would print (see below) after the call Before execution _all=[], _index=-1 next(i) 'a’ _all=['a'], _index=0 next(i) 'b’ _all=['a', 'b'], _index=1 prev(i) 'a’ _all=['a', 'b'], _index=0 #prev(i) would raise AssertionError exception next(i) 'b’ _all=['a', 'b'], _index=1 next(i) 'c’ _all=['a', 'b', 'c'], _index=2 prev(i) 'b’ _all=['a', 'b', 'c'], _index=1 next(i) 'c’ _all=['a', 'b', 'c'], _index=2 next(i) raises StopIteration exception Backwardable takes any iterable as an argument. As with other classes decorating iterators, it defines only the __init__ and __iter__ methods, with __iter__ defining its own special B_iter class for actually doing the iteration. I’ve written __init__ and __str__ for this class; do not change these. You write only the __next__, __prev__, and __clear__ methods. Here is a brief description of the attributes defined in __init__. • The _all attribute stores a list remembering all the values returned via __next__, so we can go backwards and forwards through the values already produced by Backwardable’s iterable argument. • The _iterator attribute can be passed to next to produce a new value or raise StopIteration. • The _index stores the index in _all of the value most recently returned from a call of the __next__ or __prev__ methods. It typically is incremented/decremented in calls to __next__ or __prev__. You must write the code in __next__, __prev__ , and __clear__ that coordinates these attributes to produce the behavior illustrated in the example above. How does __next__ work? Depending on value of _index and the length of _all, it might just return a value from _all; but if _index is at the end of the list, __next__ will need to call next on _iterator to get a new one to return (while also appending this new value at the end of _all). Ultimately _index must be updated as appropriate. How does __prev__ work? It just returns a value from the _all list; but it raises the AssertionError exception if an attempt is made to get a value previous to the first value produced by the iterable. How does __clear__ work? It resets the attributes, forgetting any previous values produced (but remembering the current one, if there is one); with this method, if we are done looking at the earlier part of an iterator with many values, we can forget those values and reclaim the list space storing them.

ansver
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 22:00, robert7248
Competent nonverbal communication involves interacting with others in a manner that is appropriate for which of the following? select all that apply. situation task individuals
Answers: 3
image
Computers and Technology, 22.06.2019 23:30, elizabethburkha
Which text format is this, "the text is transcribed exactly as it sounds and includes all the utterances of the speakers. "?
Answers: 2
image
Computers and Technology, 23.06.2019 03:30, 890777
In vista and windows 7, the appearance and personalization option allows you to change the
Answers: 1
image
Computers and Technology, 24.06.2019 13:00, Savtheartist23
Ais a built in formula in spread spread a is any math process such as addition or subtraction. options are function and operation
Answers: 1
You know the right answer?
The Backwardable class decorates iterables (and is itself iterable), allowing calls to both the next...

Questions in other subjects:

Konu
Mathematics, 20.02.2021 02:10
Konu
Mathematics, 20.02.2021 02:10
Konu
Mathematics, 20.02.2021 02:10