Skip to content

Instantly share code, notes, and snippets.

@lzpel
Created January 9, 2024 14:54
Show Gist options
  • Save lzpel/e72ae840150e240a9ad171ae5b57817b to your computer and use it in GitHub Desktop.
Save lzpel/e72ae840150e240a9ad171ae5b57817b to your computer and use it in GitHub Desktop.
avoid an unexpected keyword argument
from inspect import signature
from typing import Callable
import datetime
def print_time(text="this is"):
print(text, datetime.datetime.now())
def print_time_with_self(self, text="this is"):
print(text, self)
def call_method_or_function(f: Callable):
sig = signature(f)
if "self" in sig.parameters:
f(datetime.datetime.fromtimestamp(0))
else:
f()
if __name__ == "__main__":
call_method_or_function(print_time)
call_method_or_function(print_time_with_self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment