In [1]:
a = "abc"

In [2]:
dir(a)


Out[2]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

In [3]:
dir([])


Out[3]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']

In [12]:
it = [1,2,3].__iter__()

In [13]:
while(it):
    print(it.__next__())


1
2
3
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-13-72f923f37595> in <module>()
      1 while(it):
----> 2     print(it.__next__())

StopIteration: 

In [14]:
dir([].__iter__())


Out[14]:
['__class__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__length_hint__',
 '__lt__',
 '__ne__',
 '__new__',
 '__next__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__setstate__',
 '__sizeof__',
 '__str__',
 '__subclasshook__']

In [15]:
type([].__iter__())


Out[15]:
list_iterator

In [16]:
help(isinstance)


Help on built-in function isinstance in module builtins:

isinstance(obj, class_or_tuple, /)
    Return whether an object is an instance of a class or of a subclass thereof.
    
    A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
    check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
    or ...`` etc.


In [19]:
class Counter:
    def __init__(self, stop):
        self.current = 0
        self.stop = stop
        
    def __iter__(self):
        return self
    
    def __next__(self):
        if self.current < self.stop:
            r = self.current
            self.current += 1
            return r
        else:
            raise StopIteration

In [23]:
for i in Counter(4):
    print(i, end='')


0123

In [24]:
a,b,c = Counter(3)

In [25]:
print(a,b,c)


0 1 2

In [31]:
a,b,c = map(int, input().split())


1 2 3

In [9]:
class Count:
    def __init__(self, stop):
        self.stop = stop
    
    def __getitem__(self, index):
        if index < self.stop:
            return index+10
        else:
            raise IndexError

In [10]:
c = Count(3)

In [11]:
print(c[0], c[1], c[2])


10 11 12

In [5]:
for i in Count(3):
    print(i, end=' ')


0 1 2 

In [12]:
it = iter(range(3))

In [13]:
next(it)


Out[13]:
0

In [14]:
next(it)


Out[14]:
1

In [15]:
next(it)


Out[15]:
2

In [16]:
next(it)


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-16-2cdb14c0d4d6> in <module>()
----> 1 next(it)

StopIteration: 

In [17]:
import random

In [21]:
it = iter(lambda: random.randint(0,5),2)

In [28]:
next(it)


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-28-2cdb14c0d4d6> in <module>()
----> 1 next(it)

StopIteration: 

In [29]:
next(it)


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-29-2cdb14c0d4d6> in <module>()
----> 1 next(it)

StopIteration: 

In [31]:
it = iter(range(3),1)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-31-368d6c631c6c> in <module>()
----> 1 it = iter(range(3),1)

TypeError: iter(v, w): v must be callable

In [37]:
for i in iter(lambda: random.randint(0,5),2):
    print(i, end=' ')


3 5 0 4 3 3 1 1 5 

In [38]:
for i in range(10):
    print(i, end='')


0123456789

In [47]:
it = iter(range(2))

In [48]:
for i in range(20):
    print(next(it,10))


0
1
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10

In [49]:
def number_gener():
    yield 0
    yield 1
    yield 2

In [50]:
for i in number_gener():
    print(i, end=' ')


0 1 2 

In [51]:
it = number_gener()

In [53]:
it.__iter__().__next__()


Out[53]:
0

In [54]:
it = iter(number_gener())

In [57]:
next(it)


Out[57]:
2

In [58]:
def one_generator():
    yield 1
    return 'return에 지정한 값'
 
try:
    g = one_generator()
    next(g)
    next(g)
except StopIteration as e:
    print(e)    # return에 지정한 값


return에 지정한 값

In [73]:
def num_gene(stop):
    n = 0
    while n < stop:
        yield n
        n += 1

In [60]:
for i in num_gene(3):
    print(i)


0
1
2

In [62]:
def num_gene2(stop):
    for n in range(stop): #여기서 range를 쓸거면 구지 제너레이션 기법을 안해도 될거 같다.
        yield n

In [63]:
for i in num_gene2(4):
    print(i)


0
1
2
3

In [64]:
def upper_gene(x):
    for i in x:
        yield i.upper()

In [66]:
a = ['aaa','bbb']

for i in upper_gene(a):
    print(i)


AAA
BBB

In [67]:
def number_gene3():
    x = [1,2,3]
    yield from x

In [68]:
for i in number_gene3():
    print(i)


1
2
3

In [76]:
def three_gene():
    yield from num_gene(4)

In [77]:
for i in three_gene():
    print(i)


0
1
2
3

In [78]:
[ i for i in range(50) if i % 2 == 0]


Out[78]:
[0,
 2,
 4,
 6,
 8,
 10,
 12,
 14,
 16,
 18,
 20,
 22,
 24,
 26,
 28,
 30,
 32,
 34,
 36,
 38,
 40,
 42,
 44,
 46,
 48]

In [80]:
for i in ( i for i in range(50) if i % 2 == 0):
    print(i, end=' ')


0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 

In [89]:
for i in (i for i in range(50) if i % 2 == 0):
    print(i, end=' ')


0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 

In [87]:
for i in [i for i in range(50) if i % 2 == 0]:
    print(i, end=' ')


0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 

In [ ]: