Skip to content

Instantly share code, notes, and snippets.

@laixintao
Created August 21, 2017 06:15
Show Gist options
  • Save laixintao/121ef17e2ca460fb9091fcac171d0da8 to your computer and use it in GitHub Desktop.
Save laixintao/121ef17e2ca460fb9091fcac171d0da8 to your computer and use it in GitHub Desktop.
python的re在函数中编译之后,退出函数并不会立即被回收,下次会使用同样的re
# -*- coding: utf-8 -*-
import re
def clean(string):
re_strip = re.compile(r'(^\s+)|([\s::]+$)')
print id(re_strip)
result = re_strip.sub('', string)
return result
print clean(" test ,...")
print clean("\t test ,...")
# output
4503430416
test ,...
4503430416
test ,...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment