I was reading "Effective Python 2nd Edition" and summarized what I searched for.
Isn't this the case when writing Python? I have
unpack.py
#This is reassuring
x, y = point2d
#this is...Is it in order?
weather, precipitation, temperature, humidity = \
get_meteorological_info(place, time)
print(weather)
print(precipitation)
print(temperature)
print(humidity)
In Python, you can freely create unpacks and tuples anywhere, so the meaning of the code tends to be ambiguous. In this example, the order has no particular meaning. In order to improve this, it seems good to give appropriate restrictions. In Python, it can be improved by using a class that represents data.
It seems easy to understand if you can access it like this.
no_unpack.py
info_meteo = get_meteorological_info(place, time)
print(info_meteo.weather)
print(info_meteo.precipitation)
print(info_meteo.temperature)
print(info_meteo.humidity)
By returning an instance of the class, you no longer have to be aware of the order. You can do something similar with dict
, but I'm not very happy with problems such as the possibility of typos and the completion function of the editor because the key is str
etc.
There are several ways to achieve this.
It looks different in terms of implementation, but they all have in common that they are creating classes. Personally, I recommend going down.
If you know the class, you can easily create it, so I think it is an advantage that anyone can understand it. I wrote an example, but the drawback is that the description like self.weather = wheather
is redundant.
simple_class.py
class MeteologicalInfo:
def __init__(
self,
weather: str,
precipitation: float,
temperature: float,
humidity: float):
self.weather = weather
self.precipitation = precipitation
self.temperature = temperature
self.humidity = humidity
https://qiita.com/Seny/items/add4d03876f505442136
This article is detailed, but a brief summary.
I think there is no redundancy compared to when I drew the class as it is.
It is recommended to define it as a class using typing
because the editor's completion function can be used.
If you want to use it as a mutable, you may be in trouble. I think it's for small data.
use_namedtuple.py
from collections import namedtuple
#The simplest definition method
MeteologicalInfo = namedtuple(
"MeteologicalInfo",
[
"weather",
"precipitation",
"temperature",
"humidity"
]
)
#Can also be defined with whitespace
MeteologicalInfo = namedtuple(
"MeteologicalInfo",
"weather precipitation temperature humidity"
)
#You can set the default value
MeteologicalInfo.__new__.__defaults__ = ("sunny", 0.0, 25.0, 0.5)
from typing import NamedTuple
#How to define as a class
class MeteologicalInfo(NamedTuple):
weather : str
precipitation: float
temperature : float
humidity : float
#You can also set a default value
class MeteologicalInfo(NamedTuple):
weather : str = "sunny"
precipitation: float = 0.0
temperature : float = 25.0
humidity : float = 0.5
https://qiita.com/tag1216/items/13b032348c893667862a It was easy to understand how to use this. https://docs.python.org/ja/3.7/library/dataclasses.html More information is here. I will summarize it easily.
I think it is an advantage that it can be used as both mutable and immutable for named tuple. Another advantage is that you can use useful functions such as dataclasses.field. However, since it needs to be Python 3.7 or higher, it has the disadvantage that it may not be usable depending on the module or environment you are using.
use_dataclass.py
from dataclasses import dataclass
#You can use a class as a dataclass by using a decorator
@dataclass
class MeteologicalInfo:
weather : str
precipitation: float
temperature : float
humidity : float
#You can also set the default value
@dataclass
class MeteologicalInfo:
weather : str = "sunny"
precipitation: float = 0.0
temperature : float = 25.0
humidity : float = 0.5
When the data becomes complicated or the number of variables increases, you can make the disjointed data meaningful by using classes, namedtuple, dataclass, etc. If you use dict and list as they are, there will be many anxiety factors when the program becomes large and the development speed will slow down, but I think that you can think about the data structure and algorithm separately to some extent and reduce mistakes. I am.