Python NameError: global name "any" not defined

I get the following error on my server:

Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py", line 89, in get_response response = middleware_method(request) File "myproject/middleware.py", line 31, in process_request if not any(m.match(path) for m in EXEMPT_URLS): NameError: global name 'any' is not defined 

Python 2.6 is running on the server, and this error was not raised in development. The violation code is in middleware.py :

 ... if not request.user.is_authenticated(): path = request.path_info.lstrip('/') if not any(m.match(path) for m in EXEMPT_URLS): return HttpResponseRedirect(settings.LOGIN_URL) 

Should I rewrite this any function to solve this problem?

+4
source share
2 answers

In fact, you are working on Python 2.4, which does not have any built-in.

If you need to define your own any , easily:

 try: any except NameError: def any(s): for v in s: if v: return True return False 
+10
source

I also got this Python error with this line:

 >>> any([False, True, False]) Error:'any' is not defined 

This is where you work without overriding the any function:

 >>> [False, True, False].count(True) > 0 True 

Counting the number of Trues and then claiming it is greater than 0 does the same as any function. It may be slightly less efficient, as it requires a full list scan, not a break, once True is found.

0
source

Source: https://habr.com/ru/post/1393409/


All Articles