Files
python3-cookbook/cookbook/c08/p20_invoke_bystr.py

41 lines
769 B
Python
Raw Permalink Normal View History

2015-02-05 16:14:15 +08:00
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 通过字符串调用方法
Desc :
"""
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
# !r表示调用后面参数的__repr__()方法
return 'Point({!r:},{!r:})'.format(self.x, self.y)
def distance(self, x, y):
return math.hypot(self.x - x, self.y - y)
p = Point(2, 3)
d = getattr(p, 'distance')(0, 0) # Calls p.distance(0, 0)
import operator
operator.methodcaller('distance', 0, 0)(p)
points = [
Point(1, 2),
Point(3, 0),
Point(10, -3),
Point(-5, -7),
Point(-1, 8),
Point(3, 2)
]
# Sort by distance from origin (0, 0)
points.sort(key=operator.methodcaller('distance', 0, 0))