2017.12
关于python的结构体和枚举型:
class BufferPartType(Enum)
Range=
1
Intensity=
2
Confidence=
3
Undefined=
4
#define struct
class Coord3D(Structure):
_fields_ = [ (
"x"
, c_float)
,
(
"y"
, c_float)
,
(
"z"
, c_float)
,
(
"IsValid()"
,c_bool) ]
有时候我们可能需要像C中的struct那样的数据类型,把少量的数据项放在一起。Python中可以使用定义一个空类来实现这一点:
# filename:p.py
class
Employee:
pass
john = Employee()
# Create an empty employee record
# Fill the fields of the record
john.name =
'John Doe'
john.dept =
'computer lab'
john.salary =
1000
>>> import p
>>> p.john
<p.Employee instance at
0xb71f50ac>
>>> p.john.name
'John Doe'
>>> p.john.dept
'computer lab'
>>> p.john.salary
1000
http://www.iplaypy.com/wenda/wd182.html:
python中的小括号( ):代表tuple元组数据类型,元组是一种不可变序列。创建方法很简单,大多时候都是用小括号括起来的。
1 2 3 4 5 6 7 8 9 |
|
Python 元组遍历排序操作方法有对元组操作的更详细讲解。
python中的中括号[ ]:代表list列表数据类型,列表是一种可变的序列。其创建方法即简单又特别,像下面一样:
1 2 |
|
如果想创建一个空的列表,或是一个值的列表其方法同元组创建,只要使用不同括号即可。Pyhton列表list操作讲解更适合新手深入认识什么是列表。
python大括号{ }花括号:代表dict字典数据类型,字典是由键对值组组成。冒号':'分开键和值,逗号','隔开组。用大括号创建的方法如下:
1 2 3 4 |
|
字典的具体操作方法可以查看Python 映射类型字典dict操作定义,本文不做详细讲解