Pro Django 2nd读书笔记

《Python之禅》的翻译和解释

代码解读基于DJango1.5.1
https://github.com/django/django.git

python django模型内部类meta详细解释

深刻理解Python中的元类(metaclass)

如果你喜欢的话,可以把元类称为“类工厂”(不要和工厂类搞混了:D) type就是Python的内建元类,当然了,你也可以创建自己的元类。
What is a metaclass in Python?

signal dispatching

from django.db.models.signals import m2m_changed

def toppings_changed(sender, **kwargs):
    # Do something
    pass

m2m_changed.connect(toppings_changed, sender=Pizza.toppings.through)
>>> p = Pizza.objects.create(...)
>>> t = Topping.objects.create(...)
>>> p.toppings.add(t)

the arguments sent to a m2m_changed handler (toppings_changed in the example above) would be:

Argument Value

sender Pizza.toppings.through (the intermediate m2m class)

instance p(the Pizza instance being modified)

action "pre_add"(followed by a separate signal with "post_add")

reverse False(Pizza contains the ManyToManyField(so this call modifies the forward relation)

model Topping(the class of the objects added to the Pizza)

pk_set {t.id}(since only Topping t was added to the relation)
using "default"(since the default router sends writes here)

Signal.connect(receiver, sender=None, weak=True, dispatch_uid=None)
receiver function 有时候也被称为signal handler

Python中的类变量和成员变量
可以发现:python的类变量和C++的静态变量不同,并不是由类的所有对象共享。类本身拥有自己的类变量(保存在内存),当一个TestClass类的对象被构造时,会将当前类变量拷贝一份给这个对象,当前类变量的值是多少,这个对象拷贝得到的类变量的值就是多少;而且,通过对象来修改类变量,并不会影响其他对象的类变量的值,因为大家都有各自的副本,更不会影响类本身所拥有的那个类变量的值;只有类自己才能改变类本身拥有的类变量的值

如何理解 Python 的 Descriptor?
深入解析Python中的descriptor描述器的作用及用法
简明Python魔法-1
简明Python魔法-2

你可能感兴趣的:(Pro Django 2nd读书笔记)