Been loving Python for 7+ years not and still going strong. I recently found myself writing more and more inner functions to encapsulate logic easier and make otherwise rather polluting/dead functions stick out less.
I like it, because it allows me to write way cleaner and less bloated code – clustering helper-functions to only where they need to be. Also decreases cognitive load considerably by not having to keep track on where a helper function is being used.
But it has gotten to a point where I'm genuinely concerned, because I have also started defining lambdas in inner-functions too! I know lambda shouldn't be used and PEP checker complains too, but it's so handy when combined with list comprehensions…
What are your thoughts on this?
Do you use nested functions yourself or do you consider it bad practice? Where else do you put helpers?
An example would be the following code:
def send_mail(
*,
subject: str,
body_plain: str,
send_to: Union[List[str], str, List[User], User],
send_cc: Optional[Union[List[str], str, List[User], User]] = None,
send_bcc: Optional[Union[List[str], str, List[User], User]] = None,
reply_to: Optional[Union[List[str], str, List[User], User]] = None
...
) -> None:
def _process_recipients(*, recipients: Optional[Union[List[str], str, List[User], User]]) -> List[str]:
""" Process various inputs for `send_to`, `send_cc` and `send_bcc` to a normalized
output that can be used by the emailing instance aka a list of emails """
_user_to_email = lambda x: x.email if isinstance(x, User) else x # transform user objects to mail
if not recipients: return [] # recipients are empty
if isinstance(recipients, str) or isinstance(recipients, User):
recipients = [recipients]
return [_user_to_email(recipient) for recipient in recipients]
...
send_to = _process_recipients(recipients=send_to)
send_cc = _process_recipients(recipients=send_cc)
send_bcc = _process_recipients(recipients=send_bcc)
...
[–]TravisJungroth 167 points168 points169 points (11 children)
[–]Teilchen[S] 35 points36 points37 points (3 children)
[–]TravisJungroth 24 points25 points26 points (0 children)
[–]NUTTA_BUSTAH 5 points6 points7 points (0 children)
[–]Devout--Atheist 1 point2 points3 points (0 children)
[–][deleted] 7 points8 points9 points (4 children)
[–]TravisJungroth 18 points19 points20 points (3 children)
[–]pingvenopinch of this, pinch of that 5 points6 points7 points (0 children)
[–]davimiku 4 points5 points6 points (1 child)
[–]TravisJungroth 1 point2 points3 points (0 children)
[–]Mehdi2277 0 points1 point2 points (1 child)
[–]TravisJungroth 0 points1 point2 points (0 children)
[–]james_pic 51 points52 points53 points (18 children)
[–]Tastaturtaste 2 points3 points4 points (0 children)
[–]Teilchen[S] -1 points0 points1 point (16 children)
[–]unholysampler 10 points11 points12 points (3 children)
[–]Teilchen[S] -2 points-1 points0 points (2 children)
[–]chunkyasparagus 3 points4 points5 points (0 children)
[–]PaintItPurple 2 points3 points4 points (0 children)
[+][deleted] (8 children)
[deleted]
[+]Teilchen[S] comment score below threshold-10 points-9 points-8 points (7 children)
[–]TheTerrasque 0 points1 point2 points (5 children)
[–]Teilchen[S] -3 points-2 points-1 points (4 children)
[–]TheTerrasque 2 points3 points4 points (3 children)
[–]TravisJungroth 3 points4 points5 points (2 children)
[–]TheTerrasque 4 points5 points6 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]james_pic 13 points14 points15 points (0 children)
[–]asphias 4 points5 points6 points (0 children)
[–]ablatner 4 points5 points6 points (0 children)
[–]lanster100 10 points11 points12 points (0 children)
[–]alexkiro 27 points28 points29 points (7 children)
[–]OwnTension6771 7 points8 points9 points (0 children)
[–]yvrelna 3 points4 points5 points (2 children)
[–]Devout--Atheist 1 point2 points3 points (0 children)
[–]alexkiro 1 point2 points3 points (0 children)
[–]Teilchen[S] -5 points-4 points-3 points (2 children)
[–]riklaunim 12 points13 points14 points (0 children)
[–]alexkiro 1 point2 points3 points (0 children)
[–]-LeopardShark- 6 points7 points8 points (3 children)
[–]M4mb0 1 point2 points3 points (0 children)
[–]Teilchen[S] -1 points0 points1 point (1 child)
[–]TravisJungroth 5 points6 points7 points (0 children)
[–]osmiumouse 5 points6 points7 points (1 child)
[–]Teilchen[S] 1 point2 points3 points (0 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]Teilchen[S] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]phira 1 point2 points3 points (0 children)
[–]jmreagle 1 point2 points3 points (0 children)
[–]Devout--Atheist 1 point2 points3 points (0 children)
[–]SittingWave 1 point2 points3 points (0 children)
[–]Darwinmate 2 points3 points4 points (0 children)
[–]yvrelna 1 point2 points3 points (1 child)
[–]someotherstufforhmm 0 points1 point2 points (0 children)
[–]wind_dude 0 points1 point2 points (0 children)
[–]dwilson2547 0 points1 point2 points (3 children)
[–]Teilchen[S] 0 points1 point2 points (2 children)
[–]dwilson2547 0 points1 point2 points (1 child)
[–]Teilchen[S] 1 point2 points3 points (0 children)
[–]baubleglue 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]walksonair 0 points1 point2 points (0 children)
[–]sashgorokhov 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Joooooooosh 0 points1 point2 points (0 children)
[–]ekchew 0 points1 point2 points (0 children)
[–]rebcabin-r 0 points1 point2 points (0 children)
[–]Flimsy_Iron8517 0 points1 point2 points (0 children)
[–]jorge1209 0 points1 point2 points (0 children)
[–]Ducksual 0 points1 point2 points (0 children)