Created
August 26, 2016 13:06
-
-
Save caruccio/b1831f0b1ec0b5ae09b00a157e85f84a to your computer and use it in GitHub Desktop.
Default parameter value instead globals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Instead of defining a global to use inside your function, just define it as a default | |
# value of the function, specially if the value can cost some significant time | |
regex = re.compile('[^0-9]') # globals are evil (are they?) | |
def clean_zip(string): | |
return regex.sub('', string) | |
# This way we execute re.compile a single time AND have the advantage to overwrite it when necessary | |
def clean_zip(string, regex=re.compile('[^0-9]')): | |
return regex.sub('', string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment