def add(x:int, y:int) -> int:
return x + y
help(add)
Help on function add in module __main__:
add(x:int, y:int) -> int
>>> add.__annotations__
{'x': <class 'int'>, 'y': <class 'int'>, 'return': <class 'int'>}
In [14]: def spam(a, b, c, d):
...: print(a, b, c, d)
In [15]: from functools import partial
In [17]: s1 = partial(spam, 1)
In [18]: s1(2, 3, 4)
(1, 2, 3, 4)
In [19]: s1(4, 5, 6)
(1, 4, 5, 6)
In [20]: s2 = partial(spam, d=42)
In [21]: s2(4, 5, 5)
(4, 5, 5, 42)
In [24]: s3 = partial(spam, 1, 2, d=42)
In [25]: s3(5)
(1, 2, 5, 42)
In [26]: points = [ (1, 2), (3, 4), (5, 6), (7, 8) ]
In [27]: import math
In [28]: def distance(p1, p2):
...: x1, y1 = p1
...: x2, y2 = p2
...: return math.hypot(x2 - x1, y2 - y1)
In [29]: pt = (4, 3)
In [30]: points.sort(key=partial(distance, pt))
In [31]: points
Out[31]: [(3, 4), (1, 2), (5, 6), (7, 8)]
class Pair:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return 'Pair ({0.x!r}, {0.y!r})'.format(self)
def __str__(self):
return '({0.x!s}, {0.y!s})'.format(self)
p = Pair(3, 4)
p
Pair(3, 4)
print(p)
(3, 4)
_formats = {
'ymd' : '{d.year} - {d.month} - {d.day}',
'mdy' : ...,
'dmy' : ...
}
class Date:
def __init__(self, year, month, day):
...
def __format__(self, code):
if code == '':
code = 'ymd'
fmt = _formats[code]
return fmt.format(d=self)
class Person:
def __init__(self, first_name):
self._first_name = first_name
# Getter function
@property
def first_name(self):
return self._first_name
# Setter function
@first_name.setter
def first_name(self, value):
if not isinstance(value, str):
raise TypeError('Expected a string')
self._first_name = value
# Deleter function
@first_name.deleter
def first_name(self):
raise AttributeError("Can't delete attribute")
import math
class Circle:
def __init__(self, radius):
self.radius = radius
@property
def area(self):
return math.pi * self.radius ** 2
@property
def perimeter(self):
return 2 * math.pi * self.radius
c = Cirle(4.0)
c.radius
4
c.area
50.2654824
c.perimeter
25.132741228
元编程的主要目标是创建函数和类,并用他们来操纵代码。
import time
from fuctools import wraps
def timethis(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(func.__name__, end-start)
return result
return wrapper
@timethis
def countdown(n):
...
#运行起来和下面代码的效果是一样的
def countdown(n):
....
countdown = timethis(countdown)
@somedecorator
def add(x, y):
return x + y
>>> orig_add = add.__wrapped__
>>> orig_add(3, 4)
7
import spam
import imp
imp.reload(spam)
import pkgutil
data = pkgutil.get_data(__package__,'somedata.dat')
# Code to execute in an independent thread
import time
def countdown(n):
while n > 0:
print('T-minus', n)
n -= 1
time.sleep(5)
# Creat and lanch a thread
from threading import Thread
t = Thread(target=countdown, args=(10,))
t.start()
t = Thread(target=function,)
- END -
推荐阅读
大幅减少GPU显存占用:可逆残差网络(The Reversible Residual Network)
AINLP-DBC GPU 云服务器租用平台建立,价格足够便宜
关于AINLP
AINLP 是一个有趣有AI的自然语言处理社区,专注于 AI、NLP、机器学习、深度学习、推荐算法等相关技术的分享,主题包括文本摘要、智能问答、聊天机器人、机器翻译、自动生成、知识图谱、预训练模型、推荐系统、计算广告、招聘信息、求职经验分享等,欢迎关注!加技术交流群请添加AINLP君微信(id:AINLP2),备注工作/研究方向+加群目的。