2017-6-7

aprilthirty60

Table of Contents

─────────────────

1 type 和 metaclass

2 type

3 type dynamic crate

4 meta programming

5 desciptor

6 垃圾回收算法

7 lazy property

8 slots 的攔截

9 faster attribute access.

10 space savings in memory

1 type 和 metaclass

═══════════════════

meta programming 起源于 lisp,偉大的 macro 能在運(yùn)行時(shí)改變程序的執(zhí)行,

python 的源編程沒(méi)這么強(qiáng),但也很不錯(cuò)。

2 type

══════

┌────

│ def choose_class(name):

│ ????if name == 'foo':

│ ????????class Foo(object):

│ ????????????pass

│ ????????return Foo ?# 返回的是類(lèi),不是類(lèi)的實(shí)例

│ ????else:

│ ????????class Bar(object):

│ ????????????pass

│ ????return Bar

│ obj = choose_class('foo')

│ print(obj)

│ obj = choose_class('xx')

│ print(obj)

└────

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

.Foo'>

'__main__.choose_class..Bar'>

3 type dynamic crate

════════════════════

┌────

│ class Dog:

│ ????pass

│ Person = type('Person',(),{})

│ print(Dog,Person)

│ print('*'*100)

│ Person = type('Person',(),{'name':'zhangsan','age':10})

│ print(Person.name,Person.age)

│ print('*'*100)

│ Person = type('Person',(object,),{'name':'zhangsan','age':10})

│ print(Person.__mro__)

│ print('*'*100)

│ def yi(self):

│ ????print('衣......')

│ '''

│ ????attribute and method is equel in grammer,and type of object theroy

│ '''

│ Person = type('Person',(object,),{'name':'zhangsan','age':10,'yi':yi})

│ p = Person()

│ p.yi()

│ print(hasattr(Person,'name'))

│ print(hasattr(Person,'name1111'))

│ print(hasattr(Person,'yi'))

│ Person.yi(1)

│ class Dog:

│ ????def __init__(self):

│ ????????self.name = 'xx'

│ ????def eat(self):

│ ????????print('eat.......')

│ ????????#print(self.name)

│ d = Dog()

│ d.eat()

│ Dog.eat(1111)

└────

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――


****************************************************************************************************

zhangsan 10

****************************************************************************************************

(, )

****************************************************************************************************

衣…… True False True衣…… eat……. eat…….

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

4 meta programming

══════════════════

┌────

│ def upper_attr(future_class_name, future_class_parents, future_class_attr):

│ ????print(future_class_name,future_class_parents,future_class_attr)

│ ????# 遍歷屬性字典,把不是__開(kāi)頭的屬性名字變?yōu)榇髮?xiě)

│ ????newAttr = {}

│ ????for name, value in future_class_attr.items():

│ ????????if not name.startswith("__"):

│ ????????????newAttr[name.upper()] = value

│ ????# 調(diào)用 type 來(lái)創(chuàng)建一個(gè)類(lèi)

│ ????return type(future_class_name, future_class_parents, newAttr)

│ class Foo(object, metaclass=upper_attr):

│ ????bar = 'bip'

│ ????def haha(self):

│ ????????pass

│ print(hasattr(Foo, 'bar'),hasattr(Foo, 'BAR'))

│ print(Foo().BAR)

│ class UpperAttrMetaClass(type):

│ ????# 這里,創(chuàng)建的對(duì)象是類(lèi),希望能夠自定義它,所以這里改寫(xiě)__new__

│ ????# 還有一些高級(jí)的用法會(huì)涉及到改寫(xiě)__call__特殊方法,但是這里不用

│ ????def __new__(cls, future_class_name, future_class_parents, future_class_attr):

│ ????????#遍歷屬性字典,把不是__開(kāi)頭的屬性名字變?yōu)榇髮?xiě)

│ ????????newAttr = {}

│ ????????for name,value in future_class_attr.items():

│ ????????????if not name.startswith("__"):

│ ????????????????newAttr[name.upper()] = value

│ ????????# 方法 1:通過(guò)'type'來(lái)做類(lèi)對(duì)象的創(chuàng)建

│ ????????# return type(future_class_name, future_class_parents, newAttr)

│ ????????# 方法 2:復(fù)用 type.__new__方法

│ ????????# 這就是基本的 OOP 編程,沒(méi)什么魔法

│ ????????# return type.__new__(cls, future_class_name, future_class_parents, newAttr)

│ ????????# 方法 3:使用 super 方法

│ ????????return super(UpperAttrMetaClass, cls).__new__(cls, future_class_name, future_class_parents, newAttr)

│ class Foo(object, metaclass = UpperAttrMetaClass):

│ ????bar = 'bip'

│ print(hasattr(Foo, 'bar'),hasattr(Foo, 'BAR'))

│ print(Foo().BAR)

└────

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――


****************************************************************************************************

zhangsan 10

****************************************************************************************************

(, )

****************************************************************************************************

衣…… True False True衣…… eat……. eat…….

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

還有一些

[http://python-3-patterns-idioms-test.readthedocs.io/en/latest/Metaprogramming.html]

5 desciptor

═══════════

In general, a descriptor is an object attribute with“binding

behavior”, one whose attribute access has been overridden by methods

in the descriptor protocol. Those methods are __get__(), __set__(),

and __delete__(). If any of those methods are defined for an object,

it is said to be a descriptor. The default behavior for attribute

access is to get, set, or delete the attribute from an object’s

dictionary. For instance, a.x has a lookup chain starting with

a.__dict__['x'], then type(a).__dict__['x'], and continuing through

the base classes of type(a) excluding metaclasses. If the looked-up

value is an object defining one of the descriptor methods, then Python

may override the default behavior and invoke the descriptor method

instead. Where this occurs in the precedence chain depends on which

descriptor methods were defined. Note that descriptors are only

invoked for new style objects or classes (a class is new style if it

inherits from object or type). Descriptors are a powerful, general

purpose protocol. They are the mechanism behind properties, methods,

static methods, class methods, and super(). They are used throughout

Python itself to implement the new style classes introduced in version

2.2. Descriptors simplify the underlying C-code and offer a flexible

set of new tools for everyday Python programs.

descriptors are invoked by the __getattribute__ method overriding

__getattribute__ prevents automatic descriptor calls __getattribute__

is only available with new style classes and objects

object.__getattribute__ and type.__getattribute__ make different calls

to __get__. data descriptors always override instance dictionaries.

non-data descriptors may be overridden by instance dictionaries.

┌────

│ def __getattribute__(self, key):

│ ????"Emulate type_getattro() in Objects/typeobject.c"

│ ????v = object.__getattribute__(self, key)

│ ????if hasattr(v, '__get__'):

│ ???????return v.__get__(None, self)

│ ????return v

└────

像屬性(property), 方法(bound 和 unbound method), 靜態(tài)方法和類(lèi)方法都是

基于描述器協(xié)議的。

[http://pyzh.readthedocs.io/en/latest/Descriptor-HOW-TO-Guide.html]

6 垃圾回收算法

══════════════

python 的垃圾回收算法主要是引用計(jì)數(shù)和垃圾回收如國(guó)遇到

┌────

│ a =list(range(100000000000))

│ gc.get_referrers(q)

│ del a

└────

7 lazy property

═══════════════

8 slots 的攔截

══════════════

Smalltalk just has the slots. Slots are easier to optimize and make

fast with a JIT VM. If you need a class to have the functionality of

a Hashtable, you just put a Dictionary into an instance variable.

(Then you have to write some plumbing code, which is not so

convenient.) just like descriptor

[https://stackoverflow.com/questions/472000/usage-of-slots] The

special attribute __slots__ allows you to explicitly state which

instance attributes you expect your object instances to have, with

the expected results:

faster attribute access. space savings in memory.

The space savings is from

Storing value references in slots instead of __dict__. Denying

__dict__ and __weakref__ creation if parent classes deny them and you

declare __slots__.

9 faster attribute access.

══════════════════════════

┌────

│ import timeit

│ class Foo(object): __slots__ = 'foo',

│ class Bar(object): pass

│ slotted = Foo()

│ not_slotted = Bar()

│ def get_set_delete_fn(obj):

│ ????def get_set_delete():

│ ????????obj.foo = 'foo'

│ ????????obj.foo

│ ????????del obj.foo

│ ????return get_set_delete

│ print(min(timeit.repeat(get_set_delete_fn(slotted))))

│ print(min(timeit.repeat(get_set_delete_fn(not_slotted))))

└────

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

0.23086228701868095 0.24860157998045906

10 space savings in memory

══════════════════════════

The default can be overridden by defining __slots__ in a new-style

class definition. The __slots__ declaration takes a sequence of

instance variables and reserves just enough space in each instance to

hold a value for each variable. Space is saved because __dict__ is not

created for each instance. SQLAlchemy attributes a lot of memory

savings with __slots__. To verify this, using the Anaconda

distribution of Python 2.7 on Ubuntu Linux, with guppy.hpy (aka heapy)

and sys.getsizeof, the size of a class instance without __slots__

declared, and nothing else, is 64 bytes. That does not include the

__dict__. Thank you Python for lazy evaluation again, the __dict__ is

apparently not called into existence until it is referenced, but

classes without data are usually useless. When called into existence,

the __dict__ attribute is a minimum of 280 bytes additionally. In

contrast, a class instance with __slots__ declared to be () (no data)

is only 16 bytes, and 56 total bytes with one item in slots, 64 with

two. I tested when my particular implementation of dicts size up by

enumerating alphabet characters into a dict, and on the sixth item it

climbs to 1048, 22 to 3352, then 85 to 12568 (rather impractical to

put that many attributes on a single class, probably violating the

single responsibility principle there.) attrs __slots__ no slots

declared + __dict__ none 16 64 (+ 280 if __dict__ referenced) one 56

64 + 280 two 64 64 + 280 six 96 64 + 1048 22 224 64 + 3352

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,644評(píng)論 19 139
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,168評(píng)論 0 23
  • 文/洛小簡(jiǎn) 從晨風(fēng)沐到午后靜聽(tīng)破空聲爬山虎的腳印點(diǎn)點(diǎn)滴滴 滴著斑痕 夜色攬過(guò)山林沉默似冷眸春的味道 在或不在悲...
    洛小簡(jiǎn)閱讀 277評(píng)論 0 2
  • 從今天開(kāi)始 忘掉昨日的憂傷 傷也好,痛也罷 隨昨夜的晚風(fēng)吹走 今天的朝陽(yáng) 展開(kāi)新的畫(huà)卷 從今天開(kāi)始 許下善良的心愿...
    海曲三少閱讀 484評(píng)論 0 0
  • 0.1 可能你以為一切都在變質(zhì),同情心的缺失,人群的冷漠,但可能很多情況下,他們只是在內(nèi)心做足了掙扎與斗爭(zhēng)。 禮拜...
    左左左左左左嶼閱讀 439評(píng)論 2 4

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