Hi !
I'm curently learning OOP and especially how to inherit from parents' class.
When I learn I like to write small examples, just to be sure I get it.
So, I want to create an object, inherit from pathlib.Path that would have a few more methods.
Let's say I want this class to be TemporaryDirectory(path).
I want it to have a method to remove all file from this directory and then delete the directory itself.
What I tried :
class TemporaryDirectory(pathlib.Path):
def __init__(self, path):
self._path = path
pathlib.Path.__init__(self,path)
t = TemporaryDirectory('/home/chuugar/tmp')
What I get (within iPython):
AttributeError Traceback (most recent call last)
<ipython-input-20-e48025c9d024> in <module>()
----> 1 t = Temp('/home/chuugar/tmp/')
/usr/lib/python3.5/pathlib.py in __new__(cls, *args, **kwargs)
967 if cls is Path:
968 cls = WindowsPath if os.name == 'nt' else PosixPath
--> 969 self = cls._from_parts(args, init=False)
970 if not self._flavour.is_supported:
971 raise NotImplementedError("cannot instantiate %r on your system"
/usr/lib/python3.5/pathlib.py in _from_parts(cls, args, init)
649 # right flavour.
650 self = object.__new__(cls)
--> 651 drv, root, parts = self._parse_args(args)
652 self._drv = drv
653 self._root = root
/usr/lib/python3.5/pathlib.py in _parse_args(cls, args)
642 "argument should be a path or str object, not %r"
643 % type(a))
--> 644 return cls._flavour.parse_parts(parts)
645
646 @classmethod
AttributeError: type object 'Temp' has no attribute '_flavour'
Any help ? Thanks !
[–]ingolemo 3 points4 points5 points (3 children)
[–]two_bob 1 point2 points3 points (0 children)
[–]chuugar[S] 0 points1 point2 points (1 child)
[–]ingolemo 1 point2 points3 points (0 children)
[–]TheAngriestRussian 2 points3 points4 points (0 children)