4
def func():
    something

d = { 'func': func }
d['func']() # callable

d2 = { 'type': { 'func': func } }
d2['type']['func']() # not callable

d3 = { 'type': { 'func': func() } }
d3['type']['func']() # callable

What is different between d and d2 ?

Why d3 is callable and d2 is not callable ?

this code is executable but pycham highlight d2'func' and say 'dict object is not callable

  • 2
    Both `d2['func']` and `d3['func']` should throw errors. You are grabbing non-existent keys (`func`). You mean `d2['type']['func']` and `d3['type']['func']`? – Abdou Dec 02 '16 at 05:16
  • Your last two examples should not work. `"func"` is not an immediate key of either `dict()` – Christian Dean Dec 02 '16 at 05:18
  • sorry i just updated. – Oh Sung Cho Dec 02 '16 at 05:18
  • 3
    Also, what exactly is `something`? `something` could be of a callable type. – Christian Dean Dec 02 '16 at 05:19
  • 4
    The function definition is ***very important*** here. So it can't just be `do something`. – Abdou Dec 02 '16 at 05:19
  • Also, my IDE is waring me about your last example, not your second. Of course, this depends on the **_exact_** definition of `func()`. – Christian Dean Dec 02 '16 at 05:21
  • @leaf you're right. this problem depends on something in func. thanks. – Oh Sung Cho Dec 02 '16 at 05:23
  • 1
    If the function does not return anything (i.e. just `print`), `d3['type']['func']` will not play nice with the subsequent parentheses. A function without a `return` statement will return `None`. And boy, you don't want to feed `None` a set of parentheses. So, make sure to return a `function`. – Abdou Dec 02 '16 at 05:24
  • 1
    @OhSungCho It would be great if you can add stripped down version of function based on Abdou's comments and add more data to your question for reference. – Sanket Sudake Dec 02 '16 at 05:27
  • 1
    i try and it works for d1 and d2, d3 works only if `func` return a function – Skycc Dec 02 '16 at 08:53
  • 1
    Please [edit] your question to show us the _exact_ code you're running, and the _exact_ error message(s) it generates. Your [most recent edit](https://stackoverflow.com/posts/40925217/revisions#rev-arrow-b4aa010f-57d5-47c8-87d2-05db3cf31068) has "fixed" the `d2` example --- it now returns a callable (`func`) --- so either that's not really the code you're trying to run, or PyCharm isn't reporting an error on that line. Also, `d3` now works whenever `func` returns a callable, and only fails when `func` returns a non-callable (including `None`). – Kevin J. Chase Dec 03 '16 at 19:50

1 Answers1

0

Defining a function in python will make it callable. What it does upon completion will only be relevant when you actually call it (by using the ()-operator). With no definition of a return statement, the function will return None. As described here: Python -- return, return None, and no return at all.

When executing the supplied commands, it goes belly-up as soon as you try to call function func, as something is not defined. I fear that pycharm is doing some invalid highlighting. d and d2 are callable, however d3 is not. Since you call func when assigning d3, it errors here and d3 does not exist.

Python 2.7.12 (default, Oct 10 2016, 12:50:22)
[GCC 5.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/usr/lib/python2.7/lib-dynload/readline.dll", 2);
import readline # dynamically loaded from /usr/lib/python2.7/lib-dynload/readline.dll
>>>
>>> def func():
...     something
...
>>> d = { 'func': func }
>>> d['func']()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in func
NameError: global name 'something' is not defined
>>>
>>> d2 = { 'type': { 'func': func } }
>>> d2['type']['func']()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in func
NameError: global name 'something' is not defined
>>>
>>> d3 = { 'type': { 'func': func() } }
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in func
NameError: global name 'something' is not defined
>>> d3['type']['func']()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'd3' is not defined
Community
  • 1
  • 1
mic
  • 1
  • 2