Skip to content

Commit 6783cf9

Browse files
committed
nametuple
1 parent a35d008 commit 6783cf9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time    : 2020/8/3 15:57
3+
# @Author  : xiaolu
4+
# @FileName: 001-collections中的namedtuple用法.py
5+
# @Software: PyCharm
6+
7+
# 我的认识 感觉nametuple是一种便捷类的使用
8+
from collections import namedtuple
9+
10+
Point = namedtuple("Point", ['x', 'y'])
11+
# 相当于定义了一个Point类,其中x, y为类的属性
12+
p = Point(1, 2)
13+
print(p.x)
14+
print(p.y)
15+
16+
17+
# 在深度学习中 我们可以定义参数文件
18+
from collections import namedtuple
19+
Config = namedtuple('Config', ['learning_rate',
20+
'epoch',
21+
'device',
22+
'batch_size',
23+
'vocab_size'])
24+
25+
26+
config = Config(
27+
learning_rate=1e-5,
28+
epoch=10,
29+
device=4,
30+
batch_size=32,
31+
vocab_size=12239
32+
)
33+
print(config.learning_rate)
34+
35+

0 commit comments

Comments
 (0)