<ul><li>Python Objects </li></ul><ul><ul><li>Mykola Kharechko, Developer </li></ul></ul><ul><ul><li>© Quintagroup, 2008 </...
Table of contents <ul><ul><li>Objects in memory </li></ul></ul><ul><ul><li>Objects at python level </li></ul></ul><ul><ul>...
Objects in memory <ul><ul><li>Object structure </li></ul></ul><ul><ul><li>Python memory management </li></ul></ul><ul><ul>...
Object structure
Objects with same class
Python Memory Management
Allocation Strategy For small requests, the allocator sub-allocates <Big> blocks of memory. Requests greater than 256 byte...
Pymalloc structure Arena 256kb Pool 4kb Fixed size block
Fixed size block * Request in bytes Size of allocated block Size class idx * ---------------------------------------------...
Garbage Collection Python performs garbage collection via reference counting and a cyclic garbage collector that is able t...
Example >>> import gc >>> gc.disable() # 2.9mb >>> a = range(1000000) # 18mb >>> del a # 14mb >>> gc.enable() # 14mb >>> g...
Weak references <ul><ul><li>weakref module </li></ul></ul><ul><ul><li>weakref.ref class </li></ul></ul><ul><ul><li>weakref...
weakref example >>> from weakref import ref, proxy >>> class A(object):pass ... >>> a = A() ‏ >>> ref_a = ref(a) ‏ >>> a.a...
List object typedef struct { PyObject_VAR_HEAD // ob_size /* Vector of pointers to list elements. list[0] is ob_item[0], e...
List creation PyObject * PyList_New(Py_ssize_t size) ‏ { PyListObject *op; size_t nbytes; .... nbytes = size * sizeof(PyOb...
Integer objects typedef struct { PyObject_HEAD long ob_ival; } PyIntObject;
Integer optimization. Problem >>> a = -2 >>> b = -2 >>> id(a) == id(b) ‏ True >>> a = 300 >>> b = 300 >>> id(a) == id(b) ‏...
Integer optimization. Solution #define NSMALLPOSINTS 257 #define NSMALLNEGINTS 5 /* References to small integers are saved...
Objects at python level <ul><ul><li>Objects creation </li></ul></ul><ul><ul><li>Object attributes </li></ul></ul><ul><ul><...
Objects creation Constructor: obj = type->tp_new(type, args, kwds); type = Py_TYPE(obj); type->tp_init(obj, args, kwds); C...
Object attributes Attributes stored in __dict__ or in object when __slots__ used Attribute can be accessed via descriptor ...
Specific attributes <ul><ul><li>__getattribute__ -access to all attrs </li></ul></ul><ul><ul><li>__setattr__ -opposite to ...
Objects in PVM typedef struct { PyObject_HEAD int co_argcount; /* #arguments, except *args */ int co_nlocals; /* #local va...
CodeObject example File test.py a = 17 b = a + 3 print b
CodeObject Example >>> import test 34 >>> pyc = open('test.pyc') ‏ >>> pyc.read(8) ‏ 'xb3xf2
>Cx8eH' >>> from marshal imp...
CodeObject example >>> from dis import dis >>> dis(co) ‏ 1 0 LOAD_CONST 0 (17) ‏ 3 STORE_NAME 0 (a) ‏ 2 6 LOAD_NAME 0 (a) ...
Upcoming SlideShare
Loading in...5
×

Python Objects

2,153

Published on

Objects in memory, Objects at python level, Objects in python virtual machine

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,153
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
40
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Python Objects

  1. 1. <ul><li>Python Objects </li></ul><ul><ul><li>Mykola Kharechko, Developer </li></ul></ul><ul><ul><li>© Quintagroup, 2008 </li></ul></ul>
  2. 2. Table of contents <ul><ul><li>Objects in memory </li></ul></ul><ul><ul><li>Objects at python level </li></ul></ul><ul><ul><li>Objects in python virtual machine </li></ul></ul>
  3. 3. Objects in memory <ul><ul><li>Object structure </li></ul></ul><ul><ul><li>Python memory management </li></ul></ul><ul><ul><li>Garbage collection </li></ul></ul>
  4. 4. Object structure
  5. 5. Objects with same class
  6. 6. Python Memory Management
  7. 7. Allocation Strategy For small requests, the allocator sub-allocates <Big> blocks of memory. Requests greater than 256 bytes are routed to the system's allocator.
  8. 8. Pymalloc structure Arena 256kb Pool 4kb Fixed size block
  9. 9. Fixed size block * Request in bytes Size of allocated block Size class idx * ------------------------------------------------------------------------------ * 1-8 8 0 * 9-16 16 1 * 17-24 24 2 * 25-32 32 3 * 33-40 40 4 * 41-48 48 5 * 49-56 56 6 * 57-64 64 7 * 65-72 72 8 * ... ... ... * 241-248 248 30 * 249-256 256 31
  10. 10. Garbage Collection Python performs garbage collection via reference counting and a cyclic garbage collector that is able to detect and break reference cycles.
  11. 11. Example >>> import gc >>> gc.disable() # 2.9mb >>> a = range(1000000) # 18mb >>> del a # 14mb >>> gc.enable() # 14mb >>> gc.collect() # 14mb 0 >>> gc.disable() ‏ >>> a = range(1000000) # 18mb >>> a.append(a) # 18mb >>> del a # 18mb >>> gc.enable() # 18mb >>> gc.collect() # 14 mb 1 >>>
  12. 12. Weak references <ul><ul><li>weakref module </li></ul></ul><ul><ul><li>weakref.ref class </li></ul></ul><ul><ul><li>weakref.proxy class </li></ul></ul><ul><ul><li>weakref.WeakKeyDictionary </li></ul></ul>
  13. 13. weakref example >>> from weakref import ref, proxy >>> class A(object):pass ... >>> a = A() ‏ >>> ref_a = ref(a) ‏ >>> a.a = 16 >>> ref_a().a 16 >>> proxy_a = proxy(a) ‏ >>> proxy_a.a 16 >>> proxy_a <weakproxy at 0x94e25f4 to A at 0x94e0fcc> >>> del a >>> ref_a() ‏ >>> ref_a().a Traceback (most recent call last): File &quot;<stdin>&quot;, line 1, in <module> AttributeError: 'NoneType' object has no attribute 'a' >>> proxy_a <weakproxy at 0x94e23c4 to NoneType at 0x81479b8>
  14. 14. List object typedef struct { PyObject_VAR_HEAD // ob_size /* Vector of pointers to list elements. list[0] is ob_item[0], etc. */ PyObject **ob_item; // 0 <= ob_size <= allocated // len(list) == ob_size Py_ssize_t allocated; } PyListObject;
  15. 15. List creation PyObject * PyList_New(Py_ssize_t size) ‏ { PyListObject *op; size_t nbytes; .... nbytes = size * sizeof(PyObject *); ..... op->ob_item = (PyObject **) PyMem_MALLOC(nbytes); memset(op->ob_item, 0, nbytes); ... op->ob_size = size; op->allocated = size; ... }
  16. 16. Integer objects typedef struct { PyObject_HEAD long ob_ival; } PyIntObject;
  17. 17. Integer optimization. Problem >>> a = -2 >>> b = -2 >>> id(a) == id(b) ‏ True >>> a = 300 >>> b = 300 >>> id(a) == id(b) ‏ False
  18. 18. Integer optimization. Solution #define NSMALLPOSINTS 257 #define NSMALLNEGINTS 5 /* References to small integers are saved in this array so that they can be shared. The integers that are saved are those in the range -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). */ static PyIntObject * small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
  19. 19. Objects at python level <ul><ul><li>Objects creation </li></ul></ul><ul><ul><li>Object attributes </li></ul></ul><ul><ul><li>Specific attributes </li></ul></ul>
  20. 20. Objects creation Constructor: obj = type->tp_new(type, args, kwds); type = Py_TYPE(obj); type->tp_init(obj, args, kwds); Classes created by metaclasses
  21. 21. Object attributes Attributes stored in __dict__ or in object when __slots__ used Attribute can be accessed via descriptor protocol The most famous descriptor - property
  22. 22. Specific attributes <ul><ul><li>__getattribute__ -access to all attrs </li></ul></ul><ul><ul><li>__setattr__ -opposite to __getattribute__ </li></ul></ul><ul><ul><li>Descriptor protocol: __get__, __set__, __delete__ </li></ul></ul><ul><ul><li>Pickler protocol: __getstate__, __setstate__, __reduce__ </li></ul></ul>
  23. 23. Objects in PVM typedef struct { PyObject_HEAD int co_argcount; /* #arguments, except *args */ int co_nlocals; /* #local variables */ int co_stacksize; /* #entries needed for evaluation stack */ int co_flags;/* CO_..., see below */ PyObject *co_code; /* instruction opcodes */ PyObject *co_consts; /* list (constants used) */ PyObject *co_names;/* list of strings (names used) */ PyObject *co_varnames;/* tuple of strings (local variable names) */ PyObject *co_freevars;/* tuple of strings (free variable names) */ PyObject *co_cellvars; /* tuple of strings (cell variable names) */ /* The rest doesn't count for hash/cmp */ PyObject *co_filename;/* string (where it was loaded from) */ PyObject *co_name;/* string (name, for reference) */ int co_firstlineno;/* first source line number */ PyObject *co_lnotab;/* string (encoding addr<->lineno mapping) */ void *co_zombieframe; /* for optimization only (see frameobject.c) */ } PyCodeObject;
  24. 24. CodeObject example File test.py a = 17 b = a + 3 print b
  25. 25. CodeObject Example >>> import test 34 >>> pyc = open('test.pyc') ‏ >>> pyc.read(8) ‏ 'xb3xf2 >Cx8eH' >>> from marshal import load >>> co = load(pyc) ‏ >>> dir(co) ‏ [... 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames'] >>> co.co_consts (17, None) ‏ >>> co.co_names ('a', 'b') ‏
  26. 26. CodeObject example >>> from dis import dis >>> dis(co) ‏ 1 0 LOAD_CONST 0 (17) ‏ 3 STORE_NAME 0 (a) ‏ 2 6 LOAD_NAME 0 (a) ‏ 9 LOAD_CONST 0 (17) ‏ 12 BINARY_ADD 13 STORE_NAME 1 (b) ‏ 3 16 LOAD_NAME 1 (b) ‏ 19 PRINT_ITEM 20 PRINT_NEWLINE 21 LOAD_CONST 1 (None) ‏ 24 RETURN_VALUE
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×