Home > Backend Development > Python Tutorial > python中定义结构体的方法

python中定义结构体的方法

WBOY
Release: 2016-06-16 08:46:46
Original
1979 people have browsed it

Python中没有专门定义结构体的方法,但可以使用class标记定义类来代替结构体,
其成员可以在构造函数__init__中定义,具体方法如下。

复制代码 代码如下:

class item:
    def __init__(self):
        self.name = ''     # 名称
        self.size = 10     # 尺寸
        self.list = []     # 列表

a = item() # 定义结构对象
a.name = 'cup'
a.size = 8
a.list.append('water')

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template