Skip to content

Instantly share code, notes, and snippets.

View JackInTaiwan's full-sized avatar

Jack Cheng JackInTaiwan

View GitHub Profile
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))) {
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)]
### Use recursive lambda functions to define function with multiple arguments
add = lambda x: (lambda y: x + y)
print(add(3)(5))
# > 8
### Use normal function to do the same thing
def add(x, y):
return x + y
print(add(3, 5))
### Build lambda function
lambda x: x + 1
### Build lambda function and assign it into one variable
add = lambda x: x + 1
print(add(5))
# > 6
def dog():
height = 40
def grow_up():
nonlocal height
height = height + 1
def show_height():
print("Thanks for making me growing up. I'm now {} meters !!!!".format(height))
def dog():
height = 40
def grow_up():
nonlocal height
height = height + 1
print("Thanks for making me growing up. I'm now {} meters !!!!".format(height))
return grow_up
global x
x = 10
def add_x():
x = x + 1
if __name__ == "__main__":
add_x()
def dog():
height = 40
def grow_up():
height = height + 1
return grow_up
if __name__ == "__main__":
def dog():
height = 40
def profile():
print("I'm a dog and my height is {}.".format(height))
return profile
class Dog:
def __init__(self, func):
self.talent = func
def bark(self):
print("Bark !!!")
@Dog
def dog_can_pee():