Skip to content

Instantly share code, notes, and snippets.

@caruccio
Created August 26, 2016 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caruccio/b1831f0b1ec0b5ae09b00a157e85f84a to your computer and use it in GitHub Desktop.
Save caruccio/b1831f0b1ec0b5ae09b00a157e85f84a to your computer and use it in GitHub Desktop.
Default parameter value instead globals
# 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