<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Phuriwat Angkoondittaphong on Medium]]></title>
        <description><![CDATA[Stories by Phuriwat Angkoondittaphong on Medium]]></description>
        <link>https://medium.com/@acitronella?source=rss-10f26f4a7c52------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WNkOza5ZOS1_0eh-</url>
            <title>Stories by Phuriwat Angkoondittaphong on Medium</title>
            <link>https://medium.com/@acitronella?source=rss-10f26f4a7c52------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 15:59:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@acitronella/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Who said Python is easy? (The Importance of Type Hinting in Python Programming)]]></title>
            <link>https://medium.com/@acitronella/who-said-python-is-easy-the-importance-of-type-hinting-in-python-programming-33457e2e95cd?source=rss-10f26f4a7c52------2</link>
            <guid isPermaLink="false">https://medium.com/p/33457e2e95cd</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[data-type]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Phuriwat Angkoondittaphong]]></dc:creator>
            <pubDate>Tue, 09 May 2023 19:46:01 GMT</pubDate>
            <atom:updated>2023-05-09T19:46:01.267Z</atom:updated>
            <content:encoded><![CDATA[<p>Once, I was attending a class in machine learning. The homework was to train a segmentation model in PyTorch with a provided partial solution. However, the given code didn’t have comments or a proper explanation. “How do you know what to put in each parameter?” my friend asked “or at least the type of parameters”. “Usually, you could infer them from the name or, at worst, the implementation” I replied. The question still stuck in my mind, “How could it be better?” I thought.</p><pre>import os<br>import numpy as np<br>from PIL import Image<br>from torch.utils.data import Dataset<br>import albumentations as A<br><br>class CustomDataset(Dataset):<br>    def __init__(self, image_dir, mask_dir, transform=None):<br>        self.image_dir = image_dir<br>        self.mask_dir = mask_dir<br>        self.transform = transform<br>        self.images = os.listdir(image_dir)<br><br>    def __len__(self):<br>        return len(self.images)<br><br>    def __getitem__(self, index):<br>        img_path = os.path.join(self.image_dir, self.images[index])<br>        mask_path = os.path.join(self.mask_dir, self.images[index].replace(&quot;.jpg&quot;, &quot;.png&quot;))<br>        image = np.array(Image.open(img_path).convert(&quot;RGB&quot;))<br>        mask = np.array(Image.open(mask_path).convert(&quot;L&quot;), dtype=np.uint8)<br>        mask = mask - 1  # Make it start from 0<br><br>        if self.transform is not None:<br>            augmentations = self.transform(image=image, mask=mask)<br>            image = augmentations[&quot;image&quot;]<br>            mask = augmentations[&quot;mask&quot;]<br><br>        return image, mask</pre><p>The code above is the one that my friend asked me about. Specifically, at the input argument of the constructor transform. With only the method signature, we may infer that image_dir and mask_dir are strings or <a href="https://docs.python.org/3/library/pathlib.html">Path objects</a> that lead to the directory of images or masks. We may know this if we work in a context that uses images as well. In my case, it is image segmentation.</p><p>What about transform ? Again, we may get a glimpse of the context. Probably, transform is for transforming the image, like resizing, or cropping. Even if we assume everything correctly, we still don’t know which data type of inputs of transform needs to be.</p><p>Next step, we need to look at the implementation. transform has been used in the __getitem__ method. It seems transform need to be some kind of <a href="https://docs.python.org/3/library/typing.html#typing.Callable">Callable</a>(the one that can be called eg. function) variable.</p><p>Even though we seem to get close to what transform is, we(as well as my friend) may not know at all what my instructor think transform is intended to be <a href="https://albumentations.ai/docs/api_reference/core/composition/#albumentations.core.composition.Compose">albumentations.core.composition.Compose</a>. If this seems out of the blue, then yes! that’s what I feel about Python. Without documentation, Python can be extremely hard to write programing language due to a lack of typing information.</p><p>However, in Python 3.5, a feature called “type hint” was added. In short, type can be attached to any variable to give a “hint” for IDEs, third-party plugins, or most important the programmers. Nevertheless, the Python runtime never checks if the type is actually correct or not. At least, Python programmers can get a glimpse of typing information.</p><p>If we add the type hint to the code above, it will look like this (tested on Python 3.9.5 and NumPy 1.21.1).</p><pre>import os<br>import numpy as np<br>from PIL import Image<br>from torch.utils.data import Dataset<br>import albumentations as A<br>from typing import Optional<br><br>class CustomDataset(Dataset):<br>    # Optional[T] means it might be None or an instance of type T<br>    def __init__(self, image_dir: str, mask_dir: str, transform:Optional[A.core.composition.Compose]=None):<br>        self.image_dir = image_dir<br>        self.mask_dir = mask_dir<br>        self.transform = transform<br>        self.images = os.listdir(image_dir)<br><br>    def __len__(self) -&gt; int:<br>        return len(self.images)<br><br>    def __getitem__(self, index: int) -&gt; tuple[npt.NDArray, npt.NDArray[np.uint8]]:<br>        img_path = os.path.join(self.image_dir, self.images[index])<br>        mask_path = os.path.join(self.mask_dir, self.images[index].replace(&quot;.jpg&quot;, &quot;.png&quot;))<br>        image = np.array(Image.open(img_path).convert(&quot;RGB&quot;))<br>        mask = np.array(Image.open(mask_path).convert(&quot;L&quot;), dtype=np.uint8)<br>        mask = mask - 1  # Make it start from 0<br><br>        if self.transform is not None:<br>            augmentations = self.transform(image=image, mask=mask)<br>            image = augmentations[&quot;image&quot;]<br>            mask = augmentations[&quot;mask&quot;]<br><br>        return image, mask</pre><p>Which looks more intuitive. With typing information, programmers should spend their time reading the document of the type they found rather than finding what type the arguments might be.</p><p>Python is clearly not the best example when we talk about typing. The typing syntax is still actively adjusting. Consider other programming languages that have strict type-checking like Java, Haskell, etc.</p><p>In conclusion, Python is easy to write since it doesn’t force the user to get to know variable types. However, without understanding which types are intended to use in the code, the developer may have a hard time understanding the code even though they know the context. Python programmers should engrave their minds to write the type hint to increase the reusability and reproducibility of the code, especially the complex part of the program.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33457e2e95cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>