How do I get the raw representation of a string in Python?
问题 I am making a class that relies heavily on regular expressions. Let's say my class looks like this: class Example: def __init__(self, regex): self.regex = regex def __repr__(self): return 'Example({})'.format(repr(self.regex.pattern)) And let's say I use it like this: import re example = Example(re.compile(r'\d+')) If I do repr(example) , I get 'Example('\\\\d+')' , but I want 'Example(r'\\d+')' . Take into account the extra backslash where that upon printing, it appears correctly. I suppose