Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
python source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
PyPy 3.10
PyPy 3.11
PyPy 3.9
Python 3.10
Python 3.11
Python 3.12
Python 3.13
Python 3.5
Python 3.6
Python 3.7
Python 3.8
Python 3.9
Pythran 0.15
Options
Source code
from typing import overload # the @overload decorated functions are only for type checking # remove them and the code will still function (without type checking) @overload def f(v1: float) -> int: ... @overload def f(v1: str) -> str: ... @overload def f(v1: int, v2: str) -> int: ... @overload def f(v1: list, v2: float) -> str: ... # actual implementation of the overloads using isinstance checks on params # there is really just one function; no actual overloads like in C++, Java # https://elk.zone/techhub.social/@diazona/110533164231850379 def f(v1, v2 = None): if v2 is None and isinstance(v1, float): return len(str(v1)) # illustrative return values elif v2 is None and isinstance(v1, str): return v1 elif isinstance(v1, int) and isinstance(v2, str): return len(str(v1)) + len(v2) elif isinstance(v1, list) and isinstance(v2, float): return str(v1) + ", " + str(v2) else: raise TypeError("Invalid parameter types") # use the "overloads" x = f(1.235) print(type(x), x) # int 5 x = f("abc") print(type(x), x) # str ABC x = f(3, "hello") print(type(x), x) # int 6 x = f([5, "why", 10.2], 3.1416) print(type(x), x) # str [5, "why", 10.2], 3.1416 #disallowed calls x = f() #can't call: at least 1 arg needed x = f(3) # TypeError("Invalid parameter types") x = f([5, 9]) # TypeError("Invalid parameter types") x = f(3, 5, 8) #can't call: at most 2 args accepted x = f(3.12, "xyz") #TypeError("Invalid parameter types")
python source #2
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
PyPy 3.10
PyPy 3.11
PyPy 3.9
Python 3.10
Python 3.11
Python 3.12
Python 3.13
Python 3.5
Python 3.6
Python 3.7
Python 3.8
Python 3.9
Pythran 0.15
Options
Source code
from typing import overload # the @overload decorated functions are only for type checking # remove them and the code will still function (without type checking) @overload def f(v1: float) -> int: ... @overload def f(v1: str) -> str: ... @overload def f(v1: int, v2: str) -> int: ... @overload def f(v1: list, v2: float) -> str: ... # actual implementation of the overloads using argument pack # there is really just one function; no actual overloads like in C++, Java # https://elk.zone/fosstodon.org/@compilation_error/110533231689450666 def f(*args): if len(args) == 0: raise TypeError("Missing parameter") elif len(args) > 2: raise TypeError("Too many parameters") v1 = args[0] v2 = args[1] if len(args) == 2 else None match args: case (float(),): return len(str(v1)) # illustrative return values case (str(),): return v1 case (int(), str()): return len(str(v1)) + len(v2) case (list(), float()): return str(v1) + ", " + str(v2) case other: raise TypeError("Invalid parameter types") # use the "overloads" x = f(1.235) print(type(x), x) # int 5 x = f("abc") print(type(x), x) # str ABC x = f(3, "hello") print(type(x), x) # int 6 x = f([5, "why", 10.2], 3.1416) print(type(x), x) # str [5, "why", 10.2], 3.1416 #disallowed calls x = f() # TypeError("Missing parameter") x = f(3) # TypeError("Invalid parameter types") x = f([5, 9]) # TypeError("Invalid parameter types") x = f(3, 5, 8) # TypeError("Too many parameters") x = f(3.12, "xyz") # TypeError("Invalid parameter types")
python source #3
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
PyPy 3.10
PyPy 3.11
PyPy 3.9
Python 3.10
Python 3.11
Python 3.12
Python 3.13
Python 3.5
Python 3.6
Python 3.7
Python 3.8
Python 3.9
Pythran 0.15
Options
Source code
from typing import overload # the @overload decorated functions are only for type checking # remove them and the code will still function (without type checking) @overload def f(v1: float) -> int: ... @overload def f(v1: str) -> str: ... @overload def f(v1: int, v2: str) -> int: ... @overload def f(v1: list, v2: float) -> str: ... # actual implementation of the overloads using single dispatch # there can be one function for each distinct type of the first parameter # https://docs.python.org/3/library/functools.html#functools.singledispatch # https://elk.zone/hachyderm.io/@chmp/110533354334515710 import functools as ft @ft.singledispatch def f(*args): raise ValueError("call a supported overload") @f.register def _(v1: float) -> int: #invoked if called with a float arg and nothing else return len(str(v1)) # illustrative return values @f.register def _(v1: str) -> str: #invoked if called with a str arg and nothing else return v1 @f.register def _(v1: int, v2: str) -> int: #invoked if called with a int arg and one other arg of any type if isinstance(v2, str): return len(str(v1)) + len(v2) else: raise TypeError("invalid type for param v2") @f.register def _(v1: list, v2: float) -> str: #invoked if called with a list arg and one other arg of any type if isinstance(v2, float): return str(v1) + ", " + str(v2) else: raise TypeError("invalid type for param v2") # use the "overloads" x = f(1.235) print(type(x), x) # int 5 x = f("abc") print(type(x), x) # str ABC x = f(3, "hello") print(type(x), x) # int 6 x = f([5, "why", 10.2], 3.1416) print(type(x), x) # str [5, "why", 10.2], 3.1416 #disallowed calls x = f() # can't call because singledispatch x = f(3) # TypeError: first param int requires one more param x = f([5, 9]) # TypeError: first param list requires one more param x = f(3.12, "xyz") #can't call: no dispatch with 2 params where first param is float x = f(3, 5, 8) #can't call: no dispatch with 3 params where first param is int
python source #4
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
PyPy 3.10
PyPy 3.11
PyPy 3.9
Python 3.10
Python 3.11
Python 3.12
Python 3.13
Python 3.5
Python 3.6
Python 3.7
Python 3.8
Python 3.9
Pythran 0.15
Options
Source code
# This version does NOT work in Compiler Explorer # shown here for comparison only from typing import overload # the @overload decorated functions are only for type checking # remove them and the code will still function (without type checking) @overload def f(v1: float) -> int: ... @overload def f(v1: str) -> str: ... @overload def f(v1: int, v2: str) -> int: ... @overload def f(v1: list, v2: float) -> str: ... @overload def f(v1: int) -> str: ... # tracks close to overloading in C++, Java # https://multiple-dispatch.readthedocs.io/en/latest/ from multipledispatch import dispatch @dispatch(float) def f(v1: float) -> int: #invoked if called with a float arg and nothing else return len(str(v1)) # illustrative return values @dispatch(str) def f(v1: str) -> str: #invoked if called with a str arg and nothing else return v1 @dispatch(int, str) def f(v1: int, v2: str) -> int: #invoked if called with a int arg and one other arg of any type return len(str(v1)) + len(v2) @dispatch(list, float) def f(v1: list, v2: float) -> str: #invoked if called with a list arg and one other arg of any type return str(v1) + ", " + str(v2) @dispatch(int) #unlike singledispatch, can have f(int) as well as f(int, float), etc. def f(v1: int) -> int: #invoked if called with exactly one int arg return len(str(v1)) # use the "overloads" x = f(1.235) print(type(x), x) # int 5 x = f("abc") print(type(x), x) # str ABC x = f(3, "hello") print(type(x), x) # int 6 x = f([5, "why", 10.2], 3.1416) print(type(x), x) # str [5, "why", 10.2], 3.1416 x = f(3) print(type(x), x) # int 1 #disallowed calls x = f() # no 0-arg function, NotImplementedError x = f([5, 9]) # no f(list), NotImplementedError x = f(3.12, "xyz") #no f(float, str), NotImplementedError x = f(3, 5, 8) #no f(int, int, int), NotImplementedError
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree