Created
January 9, 2024 14:54
-
-
Save lzpel/e72ae840150e240a9ad171ae5b57817b to your computer and use it in GitHub Desktop.
avoid an unexpected keyword argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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