background preloader

Objects references; weakreference

Facebook Twitter

Descriptor HowTo Guide — Python v3.3a0 documentation - Iceweasel. Abstract Defines descriptors, summarizes the protocol, and shows how descriptors are called.

Descriptor HowTo Guide — Python v3.3a0 documentation - Iceweasel

Examines a custom descriptor and several built-in python descriptors including functions, properties, static methods, and class methods. Shows how each works by giving a pure Python equivalent and a sample application. Learning about descriptors not only provides access to a larger toolset, it creates a deeper understanding of how Python works and an appreciation for the elegance of its design. Definition and Introduction In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol.

The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. Python: How do I pass a variable by reference. Python: Get object by id. Counterintuitive Python behavior - comp.lang.python. How to think like a Pythonista. This is an archive of part of a thread on comp.lang.python, kept here so I can post links to it when it seems appropriate (it dealt with a question that comes up relatively often).

How to think like a Pythonista

If you want you can read the thread on google instead. In the recent past (this is being written in April 2002, for reference), a searcher after enlightenment posted the following query to comp.lang.python: Almost needless to say, it was the poster's intuition that was at fault, but he is (was) far from unique in having this sort of misconception.

Luckily for him, two more Pythonically experienced posters -- myself and Alex Martelli -- were in a particularly pedagogical mood that day¹, and wrote lengthy articles explaining in rather different ways where he was going wrong. I ranted about thinking in terms of "names, objects and bindings", something I don't do enough, and drew some ascii art diagrams explaining what was going on under the covers of the interactive sessions the OP was confused about: Peter Parente's blog. April 30, 2008 The weakref module in the Python standard library is a useful tool for creating Python references without impeding object destruction.

Peter Parente's blog

This tutorial covers the basics of weak references, and introduces a Proxy class enabling weak references to method objects. Hefty, hefty, hefty Strong references are pointers to objects that have an effect on their reference counts, and hence their lifetime and destruction. Strong references are what you see all the time when you assign an object to a variable: >>> a = [1,2,3]>>> b = a. Python Attributes and Methods. Chapter 1.

Python Attributes and Methods

New Attribute Access What is an attribute? Quite simply, an attribute is a way to get from one object to another. Apply the power of the almighty dot - objectname.attributename - and voila! You now have the handle to another object. Which object does an attribute access return, though? Hacking into Python objects internals. You know, Python represents every object using the low-level C API (or for variable-size objects) structure, so, concretely, you can cast any Python object pointer to this type; this inheritance is built by hand, every new object must have a leading macro called which defines the header for the object.

The structure is declared in Include/object.h as: typedef struct _object { PyObject_HEAD } PyObject ; and the PyObject_HEAD macro is defined as: #define PyObject_HEAD \ _PyObject_HEAD_EXTRA \ Py_ssize_t ob_refcnt; \ struct _typeobject *ob_type; … with two fields ( ) called and , representing the reference counting for the object and the type of the object. Getting the reference count (ob_refcnt) static PyObject * builtin_id ( PyObject * self , PyObject * v ) Introduction to OOP with Python. Object Oriented Programming (The image was produced by the University of Canterbury Software Engineering and Visualisation Group as part of their research on OO software visualization.)

Introduction to OOP with Python

I've been programming with Python for over two years now . I had done some procedural programming about eight years previously - but I wasn't familiar with objects or OOP. The whole design philosophy of Python encourages a clean programming style. Its basic datatypes and system of namespaces makes it easier to write elegant, modular code . These factors, and the unique block structure by indentation rule, make Python an ideal first language. In fact the basic principles of object oriented programming are relatively easy to learn. 8.11. weakref — Weak references — Python v2.7.2 documentation. New in version 2.1.

8.11. weakref — Weak references — Python v2.7.2 documentation

Source code: Lib/weakref.py The weakref module allows the Python programmer to create weak references to objects. In the following, the term referent means the object which is referred to by a weak reference. Weakref – Garbage-collectable references to objects. The weakref module supports weak references to objects.

weakref – Garbage-collectable references to objects

A normal reference increments the reference count on the object and prevents it from being garbage collected. This is not always desirable, either when a circular reference might be present or when building a cache of objects that should be deleted when memory is needed. Reference Counting in Python. Undefined EE is Extending and Embedding the Python Interpreter.API is Python/C API Reference Manual.object.h is "/...

Reference Counting in Python

/include/python2.1/object.h". Paragraphs starting with expressions like {EE 1.10} are very similar to paragraphs in the Python documentation. Conclusions Some of the Python source code documentation can be usefully duplicated in the API documentation. I find the own, borrow, steal metaphor in the Python documentation to be confusing. Summary.