This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fibonacci(): | |
| x, y = 0, 1 | |
| while True: | |
| x, y = y, x + y | |
| yield x | |
| p = fibonacci() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fibonacci(): | |
| x, y = 0, 1 | |
| while True: | |
| x, y = y, x + y | |
| yield x | |
| fib = fibonacci() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def times_two(nums): | |
| for n in nums: | |
| yield n * 2 | |
| def natural_number(maximum): | |
| x = 0 | |
| while x < maximum: | |
| yield x | |
| x += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def times_two(nums): | |
| for n in nums: | |
| yield n * 2 | |
| def natural_number(maximum): | |
| x = 0 | |
| while x < maximum: | |
| yield x | |
| x += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student: | |
| def __new__(cls, *args, **kwargs): | |
| print("Running __new__() method.") | |
| instance = object.__new__(cls) | |
| return instance | |
| def __init__(self, first_name, last_name): | |
| print("Running __init__() method.") | |
| self.first_name = first_name | |
| self.last_name = last_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Singleton_Student(object): | |
| _instance = None | |
| def __new__(cls, *args, **kwargs): | |
| print("Running __new__() method.") | |
| if not Singleton_Student._instance: | |
| Singleton_Student._instance = object.__new__(cls) | |
| return Singleton_Student._instance | |
| def __init__(self, first_name, last_name): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student(object): | |
| def __new__(cls, *args, **kwargs): | |
| print("Running __new__() method.") | |
| return None | |
| def __init__(self, first_name, last_name): | |
| print("Running __init__() method.") | |
| self.first_name = first_name | |
| self.last_name = last_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student: | |
| def __init__(self): | |
| self._score = 0 | |
| Yang = Student() | |
| print(Yang._score) | |
| # 0 | |
| Yang._score = 100 | |
| del Yang._score |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student: | |
| def __init__(self): | |
| self._score = 0 | |
| def set_score(self, s): | |
| if 0 <= s <= 100: | |
| self._score = s | |
| else: | |
| raise ValueError('The score must be between 0 ~ 100!') | |
| Yang = Student() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student: | |
| def __init__(self): | |
| self._score = 0 | |
| def set_score(self, s): | |
| if 0 <= s <= 100: | |
| self._score = s | |
| else: | |
| raise ValueError('The score must be between 0 ~ 100!') | |
| Yang = Student() |