This file contains 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
# List | |
x = ["A", "B"] | |
x.insert(0, "C") # ["C", "A", "B"] | |
x = [(1, 2), (2, 2), (2, 1)] | |
x.sort() # [(1, 2), (2, 1), (2, 2)] | |
from copy import deepcopy | |
y = x.copy() # shallow copy | |
y = deepcopy(x) # deep copy |
This file contains 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
import tensorflow as tf | |
### Save the graph g1 | |
g1 = tf.Graph() | |
with g1.as_default(): | |
a = tf.placeholder(tf.float32, name='a') | |
b = tf.Variable(initial_value=tf.truncated_normal((1,)), name='b') | |
c = tf.multiply(a, b, name='c') |
This file contains 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 MyIterator: | |
def __init__(self, max_num): | |
self.max_num = max_num | |
def __iter__(self): | |
num = 1 | |
while num <= self.max_num: | |
yield num | |
num += 1 |
This file contains 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
x = (i for i in range(2)) | |
print(type(x)) | |
# <class 'generator'> | |
print(next(x)) | |
print(next(x)) | |
print(next(x)) | |
# 0 |
This file contains 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 generator_func(value=0): | |
while value < 10: | |
value = yield value | |
value += 1 | |
generator = generator_func() | |
print('step 1') | |
print(next(generator)) | |
print('step 2') |
This file contains 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 MyIterator: | |
def __init__(self, max_num): | |
self.max_num = max_num | |
def __getitem__(self, key): | |
if key <= self.max_num: | |
return key | |
else: | |
raise IndexError |
This file contains 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 MyIterator: | |
def __init__(self, max_num): | |
self.max_num = max_num | |
self.index = 0 | |
def __iter__(self): | |
return self | |
def __next__(self): | |
self.index += 1 |
This file contains 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
x = [1, 2, 3] | |
### Method 1: use "dir()" | |
print(dir(x)) | |
# ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', | |
# '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', | |
# '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', | |
# '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', | |
# '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', | |
# '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', |
This file contains 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
import dis | |
def dog_bark(): | |
for i in range(3): | |
print('Miouwo ~~~') | |
if __name__ == '__main__': | |
dis.dis(dog_bark) |
This file contains 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
# main.py | |
if __name__ == '__main__': | |
from sample_code import x | |
print(x) | |
================================= | |
# sample_code.py | |
x = 1000 |
NewerOlder