How to can I print the name of argument from the function in Python:
def general_info(dataset):
print('Info on dataset {}:').format( ??? )
general_info(df_visits)
The output I expect:
'Info on dataset df_visits:'
How to can I print the name of argument from the function in Python:
def general_info(dataset):
print('Info on dataset {}:').format( ??? )
general_info(df_visits)
The output I expect:
'Info on dataset df_visits:'
You can get the local name of the parameter this way:
def general_info(dataset):
print(f"Info on {dataset=}")
But you cannot retrieve the name that was used in the call. It might not even have a name, or it might have more than one.
For example, what name would you expect to see printed in response to this?:
general_info("FRED")
Or for this, would you expect to see a
or b
?:
a = "FRED"
b = a
general_info(a)
The name is not an attribute of the value.
Perhaps using **kwargs
maybe helps you here, since you can treat it as any dictionary and what you actually want is to print the keys.
def general_info(**kwargs):
for k,v in kwargs.items():
print(k)
print(f'Info on dataset {k}:')
general_info(df_visits=[], some_list=[1], some_str="qwerty")