OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How does __slots__ inheritance work? (My code works and I don't know why)

  • Thread starter Thread starter Todor Naydenov
  • Start date Start date
T

Todor Naydenov

Guest
Here is the example of my implementation:

Code:
class FileHandler:
    __slots__ = ("_target_path",)

    def __init__(self, target_path: Path) -> None:
        self._target_path = target_path

class Downloader(FileHandler):
    __slots__ = ("_url", "_downloaded_bytes")

    def __init__(self, target_path: Path, url: str) -> None:
        super().__init__(target_path)
        self._url = url
        self._downloaded_bytes = 0

... There is more stuff but that is not necessary here. In short, all the methods work in the downlaoder including download() and they all use the target_path as well as the _url and the _downlaoded_bytes, now here is something funny when I print the following:

Test and results in the next field...

foo.py​


Code:
from pathlib import Path

from solutions.file.downloader import Downloader


url = "https://www.unicode.org/Public/UCD/latest/ReadMe.txt"
path = Path(__file__).parent / "unicode.txt"


def foo():
    downloader = Downloader(path, url)

    return downloader.__slots__


if __name__ == "__main__":
    foo_val = foo()
    print(foo_val) if not foo_val is None else None

--- CONSOLE OUTPUT BELOW LINE ---

Code:
('_url', '_downloaded_bytes')

--- END OF OUTPUT ---

So, it somehow appears to work. However, is my implementation correct? Also, does it still work like legitimate slots or does it decide not to use slots and uses more memory instead?

I am still a beginner so excuse my ignorance. All answers are appreciated. Thank you!
<p>Here is the example of my implementation:</p>
<pre><code>class FileHandler:
__slots__ = ("_target_path",)

def __init__(self, target_path: Path) -> None:
self._target_path = target_path

class Downloader(FileHandler):
__slots__ = ("_url", "_downloaded_bytes")

def __init__(self, target_path: Path, url: str) -> None:
super().__init__(target_path)
self._url = url
self._downloaded_bytes = 0
</code></pre>
<p>... There is more stuff but that is not necessary here. In short, all the methods work in the downlaoder including download() and they all use the target_path as well as the _url and the _downlaoded_bytes, now here is something funny when I print the following:</p>
<p>Test and results in the next field...</p>
<h1>foo.py</h1>
<pre><code>from pathlib import Path

from solutions.file.downloader import Downloader


url = "https://www.unicode.org/Public/UCD/latest/ReadMe.txt"
path = Path(__file__).parent / "unicode.txt"


def foo():
downloader = Downloader(path, url)

return downloader.__slots__


if __name__ == "__main__":
foo_val = foo()
print(foo_val) if not foo_val is None else None
</code></pre>
<p>--- CONSOLE OUTPUT BELOW LINE ---</p>
<pre><code>('_url', '_downloaded_bytes')
</code></pre>
<p>--- END OF OUTPUT ---</p>
<p>So, it somehow appears to work. However, is my implementation correct? Also, does it still work like legitimate <strong>slots</strong> or does it decide not to use slots and uses more memory instead?</p>
<p>I am still a beginner so excuse my ignorance.
All answers are appreciated.
Thank you!</p>
 

Latest posts

Top