Advertisement
elena1234

generator in Python

Feb 8th, 2022
1,375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. class Book:
  2.     def __init__(self, author, title, year):
  3.         self.author = author
  4.         self.title = title
  5.         self.year = year
  6.  
  7.  
  8. book1 = Book("Oscar Wilde", "The Picture of Dorian Gray", 1890)
  9. book2 = Book("Charles Dickens", "A Christmas Carol", 1843)
  10. book3 = Book("Charles Dickens", "Hard Times", 1854)
  11. all_Books = []
  12. all_Books.append(book1)
  13. all_Books.append(book2)
  14. all_Books.append(book3)
  15.  
  16. def generator_func_by_title(all_Books):
  17.     for i in range(len(all_Books)):
  18.         yield all_Books[i].title
  19.  
  20. for book_title in generator_func_by_title(all_Books):
  21.         print(book_title)
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement