0

I can put those about to be executed functions in to functions_results.

I get print out the results, but I couldn't get the functions itself name.

For example,

functions_results = [
    schedules(),
    schedules("2014-06-01", "2015-03-01"),
]

list_of_functions_heads = [x for x in functions_results]
for i in list_of_functions_heads:
    # this is what I want to get
    # print(i.FUNCTION_NAME) # 'schedules()'

    print(i.head())

name not work in my case

print(i.name)

    (type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute '__name__'
user3675188
  • 7,271
  • 11
  • 40
  • 76
  • 1
    You already called the `schedules` function, so you cannot get the name of the function object.. What does `schedules()` return at all to have a `.head()` method? – Martijn Pieters Jul 15 '15 at 07:21

3 Answers3

1

You can access this through function.__name__.

James
  • 1,198
  • 8
  • 14
  • __name__ not work in my case, pls see my updatem thanks – user3675188 Jul 15 '15 at 07:41
  • This is because `i` is not a function in your code. `i` is `schedules()` or `schedules("2014-06-01", "2015-03-01")`, which going by your Error message, seem to be `DataFrame` objects. You need to make sure your object is actually a function if you want to use `__name__`. – James Jul 15 '15 at 18:01
1

You can use __name__ attribute to get your function name :

Example :

>>> def my_func():
...   print 'my_func'
... 
>>> my_func.__name__
'my_func'

Read more about python modules https://docs.python.org/2/tutorial/modules.html#modules

Also note that since you have called your function within functions_results :

functions_results = [
    schedules(),
              ^ #here you called the function 
    schedules("2014-06-01", "2015-03-01"),
]

So you can not get its name in your loop. for example if in preceding example we want to get the name of function after call python will raise an AttributeError:

>>> my_func().__name__
my_func
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute '__name__'
>>> 

So for getting ride of this problem you need to put the function without calling it in your list :

functions_results = [
    schedules, 
    schedules("2014-06-01", "2015-03-01"),
]

Also note that you can not do it again for second function call :

schedules("2014-06-01", "2015-03-01")

and for getting ride of this problem too you can use hasattr(obj, '__call__') method (and callable in python3 )to check if i was a function type then get its name :

for i in list_of_functions_heads:
      if hasattr(i, '__call__'):
           print i.__name__
Mazdak
  • 105,000
  • 18
  • 159
  • 188
0

access this through function.__name__.

>>> def a():
...     pass
... 
>>> a.__name__
'a'
>>> 
sinhayash
  • 2,693
  • 4
  • 19
  • 51