Skip to content

Instantly share code, notes, and snippets.

Jack Cheng JackInTaiwan

Block or report user

Report or block JackInTaiwan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@JackInTaiwan
JackInTaiwan / python_iteration_3.py
Last active Oct 20, 2019
python_iteration_3.py
View python_iteration_3.py
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
View python_iteration_6.py
x = (i for i in range(2))
print(type(x))
# <class 'generator'>
print(next(x))
print(next(x))
print(next(x))
# 0
View python_iteration_5.py
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')
@JackInTaiwan
JackInTaiwan / python_iteration_4.py
Last active Oct 19, 2019
python_iteration_4.py
View python_iteration_4.py
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
@JackInTaiwan
JackInTaiwan / python_iteration_2.py
Last active Oct 19, 2019
python_iteration_2.py
View python_iteration_2.py
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
@JackInTaiwan
JackInTaiwan / python_iteration_1.py
Last active Oct 19, 2019
python_iteration_1.py
View python_iteration_1.py
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__',
View python_interpreter_2.py
import dis
def dog_bark():
for i in range(3):
print('Miouwo ~~~')
if __name__ == '__main__':
dis.dis(dog_bark)
View python_interpreter_1.py
# main.py
if __name__ == '__main__':
from sample_code import x
print(x)
=================================
# sample_code.py
x = 1000
View cpython_list_getitem_sample.c
static PyObject *indexerr = NULL;
PyObject *
PyList_GetItem(PyObject *op, Py_ssize_t i)
{
if (!PyList_Check(op)) {
PyErr_BadInternalCall();
return NULL;
}
if (!valid_index(i, Py_SIZE(op))) {
View python_closure_8.py
prices = [
("ice cream", 80),
("bike", 20000),
("water", 30),
]
sorted_by_price = sorted(prices, key=lambda x: x[1])
print(sorted_by_price)
# > [('water', 30), ('ice cream', 80), ('bike', 20000)]
You can’t perform that action at this time.