Pro Django 2nd讀書筆記

《Python之禪》的翻譯和解釋

代碼解讀基于DJango1.5.1
https://github.com/django/django.git

python django模型內(nèi)部類meta詳細(xì)解釋

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

如果你喜歡的話,可以把元類稱為“類工廠”(不要和工廠類搞混了:D) type就是Python的內(nèi)建元類,當(dāng)然了,你也可以創(chuàng)建自己的元類。
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 有時(shí)候也被稱為signal handler

Python中的類變量和成員變量
可以發(fā)現(xiàn):python的類變量和C++的靜態(tài)變量不同,并不是由類的所有對(duì)象共享。類本身?yè)碛凶约旱念愖兞浚ū4嬖趦?nèi)存),當(dāng)一個(gè)TestClass類的對(duì)象被構(gòu)造時(shí),會(huì)將當(dāng)前類變量拷貝一份給這個(gè)對(duì)象,當(dāng)前類變量的值是多少,這個(gè)對(duì)象拷貝得到的類變量的值就是多少;而且,通過對(duì)象來(lái)修改類變量,并不會(huì)影響其他對(duì)象的類變量的值,因?yàn)榇蠹叶加懈髯缘母北?,更不?huì)影響類本身所擁有的那個(gè)類變量的值;只有類自己才能改變類本身?yè)碛械念愖兞康闹?/p>

如何理解 Python 的 Descriptor?
深入解析Python中的descriptor描述器的作用及用法
簡(jiǎn)明Python魔法-1
簡(jiǎn)明Python魔法-2

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容