/ home / Programming / Python /

Function Overloading

Function overloading allows one to define multiple functions under the same name, taking different arguments. The task below shows the requisite code for overloading:

Python Qt/Cpp
  1. # Special thanks from jinsuun__ on #python irc
  2. from dataclasses import dataclass
  3. @dataclass
  4. class C:
  5. m_c: int
  6. isLetter : bool = False
  7. isNonCharacter : bool = False
  8. isNull : bool = False
  9. isNumber : bool = False
  10. isPrintable : bool = True
  11. isSpace : bool = False
  12. isUpper : bool = False
  13. n = 88
  14. c = C(n)
  15. match c:
  16. case C(0):
  17. c.isNull = True
  18. case C( m_c = x ) if x in range( 1, 32 ) or x == 127:
  19. c.isNonCharacter = True
  20. c.isPrintable = False
  21. case C(" "):
  22. c.isSpace = True
  23. case C(x) if x in range( ord("A"), ord("Z") + 1 ):
  24. c.isUpper = True
  25. c.isLetter = True
  26. case C(x) if x in range( ord("a"), ord("z") + 1 ):
  27. c.isUpper = False
  28. c.isLetter = True
  29. case C(x) if x in range( ord("0"), ord("9") + 1 ):
  30. c.isNumber = True
  31. case _:
  32. pass
  1. qint8 c(88);
  2. bool isLetter (false);
  3. bool isNonCharacter(false);
  4. bool isNull (false);
  5. bool isNumber (false);
  6. bool isPrintable (true );
  7. bool isSpace (false);
  8. bool isUpper (false);
  9. switch (c) {
  10. default: break;
  11. case 0 :
  12. isNull = true;
  13. [[fallthrough]];
  14. case 1 ... 31:
  15. case 127 : // Delete
  16. isNonCharacter = true;
  17. isPrintable = false;
  18. break;
  19. case ' ':
  20. isSpace = true;
  21. break;
  22. case 'A' ... 'Z':
  23. isUpper = true;
  24. [[fallthrough]];
  25. case 'a' ... 'z':
  26. isLetter = true;
  27. break;
  28. case '0' ... '9':
  29. isNumber = true;
  30. break;
  31. }

Read as:
  • Define a debug function with two arguments
  • [ whether it returns anything is unknown ]
  • T & TT [ Shorthand for Type ]
  • which implies a generic all purpose argument
  • [ whether this function alters the arguments is unknown ]
  • in which TT equals None [ Undefined ] by default.
  • After several complex conditional statements
  • [ return statements may be hidden in the definitions ]
  • if all else fails then the system exits with the message
  • 'Fatal: No Programming'.
  • Four prototypes of a void type debug function exist
  • [ They are probably defined elsewhere ]
  • whos parameter lists are combinations of
  • [ constant [ read-only ] references to ]
  • Foo and Bar structures.
  • These functions are probably for debugging Foo and Bar.

Benefits:
  • Everything is defined under one function, which some people may like because all the definitions are close together.
  • The syntax ' if T is Foo and TT is None ' is very straight forward and easy to understand.
  • You can be a lot more flexible with the default behaviour, given the dynamic nature of the language.
  • Requisite information [ gleamed from the function signature ] is provided with less lines of code
  • Decoupled, and easy to extend.
  • Logic for determining which definition to use, is handled by compiler.

Drawbacks:
  • Every definition has to exist in an extra indentation.
  • Have to sift through the definitions to read then write your relevant code.
  • The complexity of the conditional statements is relative to the amount of parameters in the function.
  • Its not obvious that this is intended for function overloading, even after glancing at the source code.
  • Bugs are very easy to create in the conditional statements. For example, a person may define some code, but mess up the conditional, and it may never fire when expected.
  • ' TT = None ' is kind of an odd red herring.
  • Difficult to create a dynamic solution using this particular idiom.

Final Thoughts