pythonSC
Given a for loop:
Can L be the following? If so, what does the loop print? If not, why not?
Given an iterable data structure L
a. How do you obtain an iterator r of L?
r = iter(L)
b. Once you have an iterator r, what can you do to get the next value?
next(r)
c. What happens when you call next® but your iterator r has finished iterating over all values of L?
StopIteration
d. Is there a limit to the number of iterators that you can create on the same iterable?
No
Assume you have
after executing these five lines
a. What is the value of L?
b. What is the value of M?
c. What is the value of D?
Recall the Vector class from the previous lecture
Suppose a class defines an __iter__() special method, and v is an instance of Vector.
a. How does Python intend that v's __iter__() special method be invoked by the programmer? Hint: not v.__iter__()
modify __getitem__()
b. What kind of object should the __iter__() method return?
custom iterator class
c. What is one simple way to implement Vector's __iter__() method, given that the iterator for Vector would essentially be the same as the iterator for the list self._v ?
return list_iterator(self)
An alternative to part 4.© is to define a class for VectorIterator, and Vector's __iter__() method would instantiate and return it. The code is as follows:
a. In VectorIterator's constructor, what is the purpose of initializing _i = 0?
to initialize the position
b. Why does VectorIterator's constructor need to set its _vec attribute to the iterable? Why isn't it enough to just keep track of its position _i?
這樣他next()的時候 就沒有辦法回傳資料了阿
c. How does Python intend that the __next__() method of a VectorIterator instance vi be invoked? Hint: not vi.__next__()
next(vi)
d. How does __next__() special method indicate that it has finished iterating all elements?
When i larger than len(self._vec) raise StopIteration
Assume Vector is iterable, rewrite the following for-loop using a while loop and explicit iter() instantiation, next(), and catching StopIteration exception:
Can any iterable object v be passed as arguments to
For the Blackjack game example, Card is declared as a class:
a. Why is it a good practice to declare class attributes such as SPADE, CLUB, HEART, and DIAMOND even though Python3 handles unicode character literals such as '♠' '♣' '♥' '♦'.
It can't be an identifer.
b. What is the purpose of special method __int__()?
For returning the value of the card.
c. Why declare a __str__() special method even though __repr__() also exists and can make a string that represents the card?
為了支援str()這個built-in function
d. Is Card class iterable? Should it be iterable?
No, it didn't define __iter()__
e. Is Card class for instantiating iterators?
No
Continuing with the BlackJack example, a separate class named Deck is also declared.
a. Is Deck an iterable? If so, is it required to implement the __getitem__() special method?
Yes, No
b. Explain how the Deck class is able to create iterators by simply returning iter(self.deck) from its __iter_() special method. Explain why this works.
._deck is a list, list is iterable.
In Single-player BlackJack,
a. What kind of object is it as created on line 6?
The iterator of Deck (list_iterator)
b. What kind of object is returned by a call to next(it) on line 7 or 18?
Card
c. Why doesn't this program have to handle the case where the iterator raises StopIteration exception when the deck is empty?
Use the list_iterator which help tp handle thx exceptioin
Is the following a function or a generator?
if fib() is a generator for Fibonacci numbers, what is the syntax for
Assume g = fib() is a generator for Fibonacci numbers, and r = iter(deck) is an iterator where deck is an instance of iterable class Deck Which of the following are allowed?