Python's namedtuples

# Why Python is Great: Namedtuples
# Using namedtuple is way shorter than
# defining a class manually:
>>> from collections import namedtuple
>>> Car = namedtuple('Car', 'color mileage')

# Our new "Car" class works as expected:
>>> my_car = Car('red', 3812.4)
>>> my_car.color
'red'
>>> my_car.mileage
3812.4

# We get a nice string repr for free:
>>> my_car
Car(color='red' , mileage=3812.4)

# Like tuples, namedtuples are immutable:
>>> my_car.color = 'blue'
AttributeError: "can't set attribute"

​ Reprint from PyTricks


 Previous
如何写出幂等性的bash脚本 如何写出幂等性的bash脚本
​ 这种情况一定发生过很多次,您编写了一个bash脚本,但是由于一个错误,脚本执行到一半退出了。你在你的系统中修复了这个错误然后再次运行了这个脚本。但是你脚本中一半的步骤很快就执行失败了,因为他们已经被安装在了你的系统中。为了
2019-08-01 Mr Wang
Next 
Merging two dicts in Python 3.5+ with a single expression Merging two dicts in Python 3.5+ with a single expression
# How to merge two dictionaries # in Python 3.5+ >>> x = {'a': 1, 'b': 2} >>> y = {'b': 3, 'c': 4} >>> z = {**x, **y}
2019-02-25 Mr Wang
  TOC