Skip to main content
added 176 characters in body
Source Link
Snakes and Coffee
  • 8.8k
  • 4
  • 40
  • 60

From my experience (and my experience only), I use this style of coding when

  1. The recursion is only useful in the larger function (not very recommended, but I have some bad habits)

  2. There needs to be preparation done for the function, but only once (instead of a flag or other switch)

One way I use it is for logging purposes, while avoiding re-logging levels

def _factorial(x):
    return 1 if x == 0 else x*_factorial(x)

@log #assuming some logging decorator "log"
def factorial(x):
    return _factorial(x)

Otherwise, log would be called for each recursive level of the factorial function, something I may not desire.

Another usage would be to resolve default arguments.


def some_function(x = None):
    x = x or set() #or whatever else
    #some stuff
    return some_function()

Would check if x is falsey for every iteration, while what I actually need is a decorator, or as an alternative:


def some_function(x = None):
   return _some_function(x if x else set())

where _some_function is the helper function.

Specifically with 2, it allows for some freedom of abstraction. If for some reason you didn't want to use a bstsearch, you could just swap it for some other function in __contains__ (and you'd also be able to reuse code in different places)

From my experience (and my experience only), I use this style of coding when

  1. The recursion is only useful in the larger function (not very recommended, but I have some bad habits)

  2. There needs to be preparation done for the function, but only once (instead of a flag or other switch)

One way I use it is for logging purposes, while avoiding re-logging levels

def _factorial(x):
    return 1 if x == 0 else x*_factorial(x)

@log #assuming some logging decorator "log"
def factorial(x):
    return _factorial(x)

Otherwise, log would be called for each recursive level of the factorial function, something I may not desire.

From my experience (and my experience only), I use this style of coding when

  1. The recursion is only useful in the larger function (not very recommended, but I have some bad habits)

  2. There needs to be preparation done for the function, but only once (instead of a flag or other switch)

One way I use it is for logging purposes, while avoiding re-logging levels

def _factorial(x):
    return 1 if x == 0 else x*_factorial(x)

@log #assuming some logging decorator "log"
def factorial(x):
    return _factorial(x)

Otherwise, log would be called for each recursive level of the factorial function, something I may not desire.

Another usage would be to resolve default arguments.


def some_function(x = None):
    x = x or set() #or whatever else
    #some stuff
    return some_function()

Would check if x is falsey for every iteration, while what I actually need is a decorator, or as an alternative:


def some_function(x = None):
   return _some_function(x if x else set())

where _some_function is the helper function.

Specifically with 2, it allows for some freedom of abstraction. If for some reason you didn't want to use a bstsearch, you could just swap it for some other function in __contains__ (and you'd also be able to reuse code in different places)

added 176 characters in body
Source Link
Snakes and Coffee
  • 8.8k
  • 4
  • 40
  • 60

From my experience (and my experience only), I use this style of coding when

  1. The recursion is only useful in the larger function (not very recommended, but I have some bad habits)

  2. There needs to be preparation done for the function, but only once (instead of a flag or other switch)

One way I can't really type examples from my phone, so I'll douse it whenis for logging purposes, while avoiding re-logging levels



def _factorial(x):
    return 1 if x == 0 else x*_factorial(x)

@log #assuming some logging decorator "log"
def factorial(x):
    return _factorial(x)

Otherwise, log would be called for each recursive level of the factorial function, something I get homemay not desire.

From my experience (and my experience only), I use this style of coding when

  1. The recursion is only useful in the larger function

  2. There needs to be preparation done for the function, but only once (instead of a flag or other switch)

I can't really type examples from my phone, so I'll do it when I get home

From my experience (and my experience only), I use this style of coding when

  1. The recursion is only useful in the larger function (not very recommended, but I have some bad habits)

  2. There needs to be preparation done for the function, but only once (instead of a flag or other switch)

One way I use it is for logging purposes, while avoiding re-logging levels



def _factorial(x):
    return 1 if x == 0 else x*_factorial(x)

@log #assuming some logging decorator "log"
def factorial(x):
    return _factorial(x)

Otherwise, log would be called for each recursive level of the factorial function, something I may not desire.

Source Link
Snakes and Coffee
  • 8.8k
  • 4
  • 40
  • 60

From my experience (and my experience only), I use this style of coding when

  1. The recursion is only useful in the larger function

  2. There needs to be preparation done for the function, but only once (instead of a flag or other switch)

I can't really type examples from my phone, so I'll do it when I get home