-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_advance.py
More file actions
49 lines (36 loc) · 1001 Bytes
/
python_advance.py
File metadata and controls
49 lines (36 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 6 13:58:40 2018
@author: 123
"""
import sys
import copy
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
@classmethod
def new_object(cls, x, y):
return cls(x, y)
def __repr__(self):
return f'x:{self.x}, y:{self.y}'
def make_object(Class, *args, **kwargs):
return Class(*args, **kwargs)
point1 = Point(1, 2)
point2 = eval(f"{'Point'}({2},{4})")
# point2 = eval('{}({},{})'.format('Point', 2, 4))
point3 = getattr(sys.modules[__name__], 'Point')(3, 6)
point4 = globals()['Point'](4, 8)
point5 = make_object(Point, 5, 10)
point6 = copy.deepcopy(point5)
point6.x = 6
point6.y = 12
point7 = point1.__class__(7, 14)
point8 = Point.new_object(8, 16)
# 闭包延迟绑定:闭包中用到的变量的值,是在内部函数被调用的时候才查询得到的
def squares():
return [lambda x, i=i: i**x for i in range(3)]
'''
for square in squares():
print(square(2))
'''