I have a function where I'm searching for regex patterns in sentences and file paths. To do this, I need to chunk the text so that it evaluates full words, not individual characters.
def print_word_and_match_fragment(text):
word_list = text.split('\\') if '\\' in text else text.split()
patterns = re.compile(r'^[a,A][b,B]{2,3}$')
d = dict()
for i in word_list: # Does something
To me, it doesn't seem right that I have to make a local ternary variable within the function to do what I need it to do
Why can't it be:
def print_word_and_match_fragment(text = text.split('\\') if '\\' in text else text.split() ):
patterns = re.compile(r'^[a,A][b,B]{2,3}$')
d = dict()
for i in text: # Does something
Is there a way I could just call the ternary as the function parameter or at least something that's equivalent? It doesn't have to be a ternary. Some vague ideas I have was maybe using **args
or :=
.