ist1与list2相互引用,如果不存在其他对象对它们的引用,list1与list2的引用计数也仍然为1,所占用的内存永远无法被回收,这将是致命的。
对于如今的强大硬件,缺点1尚可接受,但是循环引用导致内存泄露,注定python还将引入新的回收机制。(标记清除和分代收集)
list1 = []
list2 = []
list1.append(list2)
list2.append(list1)
乍一看,Python的GC算法貌似远胜于Ruby的:宁舍洁宇而居秽室乎?为什么Ruby宁愿定期强制程序停止运行,也不使用Python的算法呢?
然而,引用计数并不像第一眼看上去那样简单。有许多原因使得不许多语言不像Python这样使用引用计数GC算法:
首先,它不好实现。Python不得不在每个对象内部留一些空间来处理引用数。这样付出了一小点儿空间上的代价。但更糟糕的是,每个简单的操作(像修改变量或引用)都会变成一个更复杂的操作,因为Python需要增加一个计数,减少另一个,还可能释放对象。
第二点,它相对较慢。虽然Python随着程序执行GC很稳健(一把脏碟子放在洗碗盆里就开始洗啦),但这并不一定更快。Python不停地更新着众多引用数值。特别是当你不再使用一个大数据结构的时候,比如一个包含很多元素的列表,Python可能必须一次性释放大量对象。减少引用数就成了一项复杂的递归过程了。
最后,它不是总奏效的。
乍眼一看,Ruby和Python的GC实现是截然不同的,Ruby使用John-MaCarthy的原生“标记并清除”算法,而Python使用引用计数。但是仔细看来,可以发现Python使用了些许标记清除的思想来处理循环引用,而两者同时以相似的方式使用基于代的垃圾回收算法。Python划分了三代,而Ruby只有两代。
http://www.word666.com/tags-python-0.html
from django.contrib.auth.decorators import user_passes_test
def group_required(*group_names):
"""Requires user membership in at least one of the groups passed in."""
def in_groups(u):
if u.is_authenticated():
if bool(u.groups.filter(name__in=group_names)) | u.is_superuser:
return True
return False
return user_passes_test(in_groups)
# The way to use this decorator is:
@group_required(‘admins’, ‘seller’)
def my_view(request, pk)
...
def anonymous_required(function=None, redirect_url=None):
if not redirect_url:
redirect_url = settings.LOGIN_REDIRECT_URL
actual_decorator = user_passes_test(
lambda u: u.is_anonymous(),
login_url=redirect_url
)
if function:
return actual_decorator(function)
return actual_decorator
# The way to use this decorator is:
@anonymous_required
def my_view(request, pk)
...
from django.core.exceptions import PermissionDenied
def superuser_only(function):
"""Limit view to superusers only."""
def _inner(request, *args, **kwargs):
if not request.user.is_superuser:
raise PermissionDenied
return function(request, *args, **kwargs)
return _inner
# The way to use this decorator is:
@superuser_only
def my_view(request):
...
from django.http import HttpResponseBadRequest
def ajax_required(f):
"""
AJAX request required decorator
use it in your views:
@ajax_required
def my_view(request):
....
"""
def wrap(request, *args, **kwargs):
if not request.is_ajax():
return HttpResponseBadRequest()
return f(request, *args, **kwargs)
wrap.__doc__=f.__doc__
wrap.__name__=f.__name__
return wrap
# The way to use this decorator is:
@ajax_required
def my_view(request):
...
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
print('%r (%r, %r) %2.2f sec' % (method.__name__, args, kw, te - ts))
return result
return timed
# The way to use this decorator is:
@timeit
def my_view(request):
...
from django.http import HttpResponseForbidden
logger = logging.getLogger(__name__)
def user_can_write_a_review(func):
"""View decorator that checks a user is allowed to write a review, in negative case the decorator return Forbidden"""
@functools.wraps(func)
def wrapper(request, *args, **kwargs):
if request.user.is_authenticated() and request.user.points < 10:
logger.warning('The {} user has tried to write a review, but does not have enough points to do so'.format( request.user.pk))
return HttpResponseForbidden()
return func(request, *args, **kwargs)
return wrapper
https://www.nowcoder.com/discuss/136491?type=2&order=3&pos=70&page=1
https://changchen.me/blog/20170503/django-performance-and-optimisation/
http://www.kancloud.cn:8080/digest/pieces-algorithm/163624