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
import time | |
from functools import wraps | |
def timer(func): | |
"""A decorator that prints how long a function took to run.""" | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
# When wrapper() is called, get the current time. | |
t_start = time.time() |
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 counter(func): | |
def wrapper(*args, **kwargs): | |
wrapper.count += 1 | |
# Call the function being decorated and return the result | |
return func(*args, **kwargs) | |
wrapper.count = 0 | |
# Return the new decorated function | |
return wrapper | |
# Decorate foo() with the counter() decorator |
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 print_return_type(func): | |
# Define wrapper(), the decorated function | |
def wrapper(*args, **kwargs): | |
# Call the function being decorated | |
result = func(*args, **kwargs) | |
print('{}() returned type {}'.format( | |
func.__name__, type(result) | |
)) | |
return result | |
# Return the decorated function |
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
# Docstring - Google style | |
def function(arg_1, arg_2=42, some_function): | |
"""Descriptioin of what this function does. | |
Args: | |
arg_1 (str): Description of arg_1. | |
(The string to search). | |
arg_2 (int, optional): Write optional when | |
argument has default value. | |
some_function (callable): The function we want a tooltip for. |
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
int call(int rnum){ | |
// rnum is parameter | |
printf("the num is %d", rnum); | |
} | |
int num = 20; | |
call(num); | |
// num is argument |
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 infinite_sequence(): | |
"""Makes the infinite sequence with Python generator. | |
""" | |
num = 0 | |
while True: | |
yield num | |
num += 1 |
NewerOlder