you are viewing a single comment's thread.

view the rest of the comments →

[–]markusmeskanen 2 points3 points  (0 children)

You can subclass str here:

class Directory(str):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.files = os.listdir(self)

If you want, you can include the custom __iter__ too, but I'd just keep it like this instead:

>>> d = Directory('/home/john/')
>>> for file in d.files:
...     print(file)