Skip to main content
added 643 characters in body
Source Link

You could go for an immutable class, perhaps a NamedTuple, or a frozen dataclass. This will also give you some useful methods for free (such as hashing / repr / eq).

As something simpler, if you just want to extend an object with a few extra methods, you have the option of subclassing Path. Generally I'd advise composition over inheritance, but if you keep your heirarchy one deep + avoid clashing with Path's internals, it's not a bad way to go.

Edit:

Actually, given that you only want to add one bit of functionality here, perhaps a decorator is the right way to go? Something like:

from functools import wraps


def check_path_exists(f):
    @wraps(f)
    def inner(directory, *args, **kwargs):
        # insert directory exists code here
        return f(directory, *args, **kwargs)
    return inner

And then you can use that wherever you like:

 @check_path_exists
 def find_files(directory: str):
    return Path(directory).glob('some_pattern')

 @check_path_exists
 def foo(directory: str):
    return

You could go for an immutable class, perhaps a NamedTuple, or a frozen dataclass. This will also give you some useful methods for free (such as hashing / repr / eq).

As something simpler, if you just want to extend an object with a few extra methods, you have the option of subclassing Path. Generally I'd advise composition over inheritance, but if you keep your heirarchy one deep + avoid clashing with Path's internals, it's not a bad way to go.

You could go for an immutable class, perhaps a NamedTuple, or a frozen dataclass. This will also give you some useful methods for free (such as hashing / repr / eq).

As something simpler, if you just want to extend an object with a few extra methods, you have the option of subclassing Path. Generally I'd advise composition over inheritance, but if you keep your heirarchy one deep + avoid clashing with Path's internals, it's not a bad way to go.

Edit:

Actually, given that you only want to add one bit of functionality here, perhaps a decorator is the right way to go? Something like:

from functools import wraps


def check_path_exists(f):
    @wraps(f)
    def inner(directory, *args, **kwargs):
        # insert directory exists code here
        return f(directory, *args, **kwargs)
    return inner

And then you can use that wherever you like:

 @check_path_exists
 def find_files(directory: str):
    return Path(directory).glob('some_pattern')

 @check_path_exists
 def foo(directory: str):
    return
Source Link

You could go for an immutable class, perhaps a NamedTuple, or a frozen dataclass. This will also give you some useful methods for free (such as hashing / repr / eq).

As something simpler, if you just want to extend an object with a few extra methods, you have the option of subclassing Path. Generally I'd advise composition over inheritance, but if you keep your heirarchy one deep + avoid clashing with Path's internals, it's not a bad way to go.