On 12/06/11 10:47:01, Steven D'Aprano wrote:
On Sat, 11 Jun 2011 21:28:40 -0500, Andrew Berg wrote:
On 2011.06.11 09:13 PM, Steven D'Aprano wrote:
A second, more subtle risk: not all objects have a __dict__. But if you
obey the rule about never updating from arbitrary objects you don't
know, then you won't be surprised by an object with no __dict__.
What objects don't (other than the obvious ones like strings,
dictionaries, ints and lists)?
namedtuple is another common example.
In pure Python, objects created using __slots__ usually don't have a
__dict__. Quite possibly C extension objects. There may be others.
The base class 'object' is a well-known example:
>>> ham = object()
>>> ham.spam = 'eggs'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'spam'
>>>
But subclasses of object do have a __dict__ by default,
which is why you'd normally do:
>>> class Ham(object):
... pass
...
>>> ham = Ham()
>>> ham.spam = 'eggs'
>>>
Here, the only reason for creating a subclass with no visible new
methods or attributes, is the unmentioned __dict__ attribute that
Ham instances have and 'object' instances lack.
-- HansM
--
http://mail.python.org/mailman/listinfo/python-list