Python Forum
TypeError: 'NoneType' object is not callable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 'NoneType' object is not callable
#1
hi
when I ran the below code, I encountered with an error message. I searched the net and read some pages but I did not understand how to correct the code. any guidance appreciated
code:
## from: ## https://www.shabakeh-mag.com/workshop/20801/ ## %D8%AF%DA%A9%D9%88%D8%B1%D8%A7%D8%AA%D9%88%D8%B1%D9 ## %87%D8%A7-%D8%AF%D8%B1-%D9%BE%D8%A7%DB%8C%D8%AA%D9% ## 88%D9%86-%D9%88-%D9%86%D8%AD%D9%88%D9%87-%D9%BE%DB% ## 8C%D8%A7%D8%AF%D9%87%E2%80%8C%D8%B3%D8%A7%D8%B2%DB% ## 8C-%D8%A2%D9%86%E2%80%8C%D9%87%D8%A7 import time def memorize(func): cash={} def wrapper(*args): if args in cash: return cash[args] else: result=func(*args) cash[atgs]=result return result return wrapper def timed(func): def wrapper(*args): start= time.time() result=func(*args) end=time.time() print(f"Elapse time: {end - start:.6f seconds}") return result return wrapper @memorize @timed def sum_of_odd_numbers(n): return sum(filter(lambda x: x%2==1,range(n))) ##if callable(sum_of_odd_numbers(10)): ## print(" sum of odd numbers of 10: ",end="") ## print( sum_of_odd_numbers(10) ) ##else: ## print("The sum_of_odd_numbers(10) is not callable.") print(" sum of odd numbers of 10: ",end="") print( sum_of_odd_numbers(10) )
after running:
Error:
Traceback (most recent call last): File "D:\14020412_enteghal\akb_python\akb_py_projects\combined_decorator_example.py", line 42, in <module> if callable(sum_of_odd_numbers(10)): TypeError: 'NoneType' object is not callable
Reply
#2
Both your decorators have errors
def memorize(func): cash={} def wrapper(*args): if args in cash: return cash[args] else: result=func(*args) cash[atgs]=result return result return wrapper # <-- Indentation error def timed(func): def wrapper(*args): start= time.time() result=func(*args) end=time.time() print(f"Elapse time: {end - start:.6f seconds}") # <- Format error return result return wrapper
Test your decorators one at a time.
akbarza likes this post
Reply
#3
Quote: I searched the net and read some pages but I did not understand how to correct the code.

I googled "TypeError: 'NoneType' object is not callable" and this was the very first hit.

https://www.geeksveda.com/typeerror-none...-callable/
“TypeError: 'nonetype' object is not callable” occurs when you try to call a None value as if it were a function. To solve it, make sure that you do not override the names of any functions with a None value.

This is exactly what you did here.
def memorize(func): cash={} def wrapper(*args): if args in cash: return cash[args] else: result=func(*args) cash[atgs]=result return result return wrapper # <-- Indentation error
Because of the indentation error, calling memorize(sum_of_odd_numbers) returns None when you wanted it to return sum_ov_numbers.wrapper. This means when you called sum_of_odd_numbers(10) you ended up calling None(10).
akbarza likes this post
Reply
#4
Noticed another error:
def memorize(func): cash={} def wrapper(*args): if args in cash: return cash[args] else: result=func(*args) cash[atgs]=result # <- Should be args, not atgs return result return wrapper # <- Indenting is still wrong
akbarza likes this post
Reply
#5
The site you get code from use plain HTML without even a <code> tag to display the code this is bad.
Usually if make a site where people post a lot of code,then have some kind of code box.
If look at source eg:
<span style="background-color:#ffffcc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cache[args] = result</span>
A overflow of &nbsp💥
So can get strange result if try to copy code,look like you have written the code over as there are typo's to.
The code work,here a cleaned up versions and i made a few changes.
import time def memoize(func): cache = {} def wrapper(*args): if args not in cache: cache[args] = func(*args) return cache[args] return wrapper def timed(func): def wrapper(*args): start = time.time() result = func(*args) end = time.time() elapsed_time = end - start print(f"Elapsed time: {elapsed_time:.6f} seconds") return result return wrapper @memoize @timed def sum_of_odd_numbers(n): return sum(x for x in range(n) if x % 2 == 1) print(sum_of_odd_numbers(10000000)) print(sum_of_odd_numbers(10000000))
Output:
Elapsed time: 0.759998 seconds 25000000000000 25000000000000
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 4,815 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
Question TypeError: argument of type 'NoneType' is not iterable Tajaldeen 7 6,118 Nov-29-2024, 09:45 AM
Last Post: Tajaldeen
  I am getting this TypeError: 'TreasureMap' object is not subscriptable. makilakos 2 2,098 May-25-2024, 07:58 PM
Last Post: deanhystad
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 4,250 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 2,848 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 5,071 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 8,392 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 3,253 Jul-23-2023, 02:25 PM
Last Post: Fare
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 5,119 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  TypeError: 'float' object is not callable #1 isdito2001 1 2,522 Jan-21-2023, 12:43 AM
Last Post: Yoriz

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.