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