python struct包_Python istruct包_程序模块 - PyPI - Python中文网

快速启动

首先,创建一个名为person的istruct对象,其中first_name和last_name是必需的,而middle_name、dob和email是可选的(指定了默认值)。>>> from istruct import istruct

>>> person = istruct("first_name", "last_name", middle_name="", dob="2000-01-01", email=None)

然后,使用first_name、last_name和middle_name创建person的实例。>>> p = person(first_name="Jim", last_name="Raynor", middle_name="Eugene")

>>> p

istruct(first_name='Jim', last_name='Raynor', email=None, dob='2000-01-01', middle_name='Eugene')

您可以像通常那样检索字段值。>>> p.first_name

'Jim'

>>> p.dob

'2000-01-01'

p是不可变的,这意味着在创建后不能对其进行修改。因此,下面这样的set/delete操作将失败,引发一个AttributeError。>>> p.first_name = "James"

Traceback (most recent call last):

File "", line 1, in

AttributeError: can't set attribute

>>> del p.email

Traceback (most recent call last):

File "", line 1, in

AttributeError: can't delete attribute

istruct只接受命名/关键字参数。它严格禁止按设计使用位置参数。>>> p = person("Jim", "Raynor")

Traceback (most recent call last):

File "", line 1, in

File "/Users/microamp/src/microamp/istruct/istruct/__init__.py", line 52, in _istruct

"(%d found)" % (len(positional),))

TypeError: No positional arguments are allowed in istruct (2 found)

istruct将在省略一个或多个必需的字段时引发TypeError。>>> p = person(last_name="Raynor")

Traceback (most recent call last):

File "", line 1, in

File "/Users/microamp/src/microamp/istruct/istruct/__init__.py", line 56, in _istruct

return nt(**merge_dicts(kwargs, attrs))

TypeError: __new__() missing 1 required positional argument: 'first_name'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值