I'm not sure if the title can express what I want to do correctly, but the point is that sometimes I want to do something like this. Especially after writing JS. ..
d = dict(a=1, b=2)
#Usually used like this
d["c"] = d["a"] + d["b"]
#I like this feeling
d.c = d.a + d.b
You can extend the dictionary a little and map __setattr__
to __setitem__
(get too). Happy!
By becoming an attribute, completion will work on IPython. For example, if you are using the requests module, when you say "I want to see the contents of the current response header!", Candidates will be displayed in a row just by tabs.
import requests
from sklearn.datasets.base import Bunch as dict
#Try hitting the GitHub API
response = requests.get("https://api.github.com")
headers = dict(**response.headers)
If you do header. <TAB>
in this state ...
Because of this, the number of strokes can be reduced considerably compared to hitting headers [" X-RateLimit-Remaining"]
etc. one by one. Wow! !! from··
Oh ... oh ...
Recommended Posts