Is this code correct in python?
def foo(flag):
if flag:
def bar():
# Somthing
else:
def bar():
# Somthing else
bar()
foo(True)
foo(False)
if not what is a recommended way to set behavior of some function (bar) under? condition?
OK The real code is following
# Building replaceFunc based of ignore_case and use_regexp flags
if not ignore_case:
if not use_regexp:
def replaceFunc(string, search, replace):
return string.replace(search, replace)
else:
def replaceFunc(string, search, replace):
pattern = re.compile(search)
return pattern.sub(replace, string)
else:
if not use_regexp:
# There is no standard puthon function for replacing string by ignoring case
def replaceFunc(string, search, replace):
# implementation from http://stackoverflow.com/questions/919056/case-insensitive-replace
return string
else:
def replaceFunc(string, search, replace):
pattern = re.compile(search, re.IGNORECASE)
return pattern.sub(replace, string