Python Tricks #6 - Namedtuples by boyanpro

View this thread on steempeak.com
· @boyanpro · (edited)
$0.52
Python Tricks #6 - 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"
```
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id73,523,345
authorboyanpro
permlinkpython-tricks-5-namedtuples
categoryprogramming
json_metadata{"community":"busy","app":"partiko","format":"markdown","tags":["programming","python","pytricks","coding","dev","partiko"],"users":[],"links":[]}
created2019-04-22 07:03:24
last_update2019-04-22 07:25:42
depth0
children0
net_rshares1,062,762,320,758
last_payout2019-04-29 07:03:24
cashout_time1969-12-31 23:59:59
total_payout_value0.408 SBD
curator_payout_value0.116 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length508
author_reputation1,773,735,625,367
root_title"Python Tricks #6 - Namedtuples"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (35)