10
Using pathlib in constant?
(lemmy.world)
Welcome to the Python community on the programming.dev Lemmy instance!
Past
November 2023
October 2023
July 2023
August 2023
September 2023
If it's a path, you should use
Path
. If it's a regular expression, define it as such withre.compile()
. If the purpose of such a module is to reduce boilerplate, then defining these values as strings only necessitates boilerplate later on to convert them.There's one edge case to consider, though it's very "edge". If you define a very long/complex regex as a constant and then import from that file, Python will run
.compile()
on import. If you're not actually using that regex though (say you imported the file to use a different value) then you're burning CPU there for no reason.I generally don't recommend that you concern yourself with that sort of thing though until you run into real performance problems. Most regexes you compile take no time at all, and the benefit of storing everything as the object you'd expect has big benefits for developer cognitive load.
@danielquinn agreed! You can work around the performance issues by wrapping it in a function.