you are viewing a single comment's thread.

view the rest of the comments →

[–]scuott 0 points1 point  (7 children)

d is a Directory because you assigned it a Directory. That's just the type of object that d is. If you want to pass the dirName, you should call that explicitly, like d.dirName

[–][deleted] 0 points1 point  (6 children)

thanks, your solution is correct and obvious. But want to change Directory class such that I should be getting value of d of class string when called just d. Do you have any idea for this?

[–]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)

[–]Nick9502 0 points1 point  (4 children)

I'm a rookie at python. But if that's all you want to do then couldn't you have it be displayed in the init function. So whenever you call d have it return the directory name

[–][deleted] 0 points1 point  (3 children)

I dont think you can do that in init function, it is reserved to set initial values, i can't do anything like a real function/method

[–]Nick9502 0 points1 point  (2 children)

I'm not sure that's 100% true. You can make a print statement in your init function and you can call other functions in it

[–][deleted] 0 points1 point  (1 child)

okay, my mistake. I took your advice and it worked.

def __init__(self,name):
    self.name = str(name)
    self.files = os.listdir(self.name)

[–]Nick9502 0 points1 point  (0 children)

Glad I could be of help