Python – Variable function name, tricks on the eyes

I was doing some lessons and this one caught my evil mind. This could be used to dynamically call various functions or to trick the eyes. Basically you make a variable with a function name then calling that function with the variable name. So in my testing code redm() is a strange looking function, but its really just print(). You can do this in many other programming languages, I used it a bunch in the past.

redm = print 
def cleanup(dirt):
    redm('Cleaning up ' + dirt)   
infoxz = cleanup   
testn = infoxz('poop')

redn = ' Yes it does.'
redm(f'Does this work? {redn}')

I just remembered a time I used a similar technique was in a ‘smallest code” contest, I would rename many longer functions with tiny new names like ‘_1’ if it was used more than once. I am posting a small snippet of this code which is mSL and non python below. This code is about 24 years old and was a small piece of a RGB hex #value selection gui, you will see this in browser extensions to pick a color value from the page you are viewing etc., btw it still works@!!

// mSL is very very similar to php and javascript

alias -l _.r _12 105 11 40 15 | $_21 -rbc $+ $1 $_27 $_2 $_3 $_4 105 11 40 15 R: $gettok($rgb($_10),1,44) | _14 $_10 | _13 $trgb.hex
alias -l _.g _12 105 30 40 15 | $_21 -rbc $+ $1 $_27 $_2 $_3 $_4 105 30 40 15 G: $gettok($rgb($_10),2,44) | _14 $_10 | _13 $trgb.hex
alias -l _.b _12 105 49 40 15 | $_21 -rbc $+ $1 $_27 $_2 $_3 $_4 105 49 40 15 B: $gettok($rgb($_10),3,44) | _14 $_10 | _13 $trgb.hex
on *:keydown:@TRGB:*:{
  if ($keyval == 40) { _15 }
  elseif ($keyval == 38) { _16 }
  elseif ($keyval == 39) { _17 }
  elseif ($keyval == 37) { _18 }
}
alias -l _15 {
  if (%_.k == r) { _.r | %_.k = g | _.g o }
  elseif (%_.k == g) { _.g | %_.k = b | _.b o }
  else { _.b | %_.k = r | _.r o }
}
alias -l _16 {
  if (%_.k == r) { _.r | %_.k = b | _.b o }
  elseif (%_.k == g) { _.g | %_.k = r | _.r o }
  else { _.b | %_.k = g | _.g o }
}

Leave a Comment