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 能在運行時改變程序的執行,

python 的源編程沒這么強,但也很不錯。

2 type

══════

┌────

│ def choose_class(name):

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

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

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

│ ????????return Foo ?# 返回的是類,不是類的實例

│ ????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)

│ ????# 遍歷屬性字典,把不是__開頭的屬性名字變為大寫

│ ????newAttr = {}

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

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

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

│ ????# 調用 type 來創建一個類

│ ????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):

│ ????# 這里,創建的對象是類,希望能夠自定義它,所以這里改寫__new__

│ ????# 還有一些高級的用法會涉及到改寫__call__特殊方法,但是這里不用

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

│ ????????#遍歷屬性字典,把不是__開頭的屬性名字變為大寫

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

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

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

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

│ ????????# 方法 1:通過'type'來做類對象的創建

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

│ ????????# 方法 2:復用 type.__new__方法

│ ????????# 這就是基本的 OOP 編程,沒什么魔法

│ ????????# 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), 靜態方法和類方法都是

基于描述器協議的。

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

6 垃圾回收算法

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

python 的垃圾回收算法主要是引用計數和垃圾回收如國遇到

┌────

│ 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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

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