__dict__ attribute for built-in types

Hrvoje Niksic hniksic at xemacs.org
Thu Oct 27 18:57:36 EDT 2011


candide <candide at free.invalid> writes:

> But beside this, how to recognise classes whose object doesn't have a
> __dict__ attribute ?

str, list and others aren't classes, they are types.  While all
(new-style) classes are types, not all types are classes.  It's
instances of classes (types created by executing the "class" statement
or its equivalent) that automatically get a __dict__, unless __slots__
was used at class definition time to suppress it.  Built-in and
extension types can choose whether to implement __dict__.

(Mechanics of defining built-in and extension types are of course
implementation-specific.  CPython allows adding __dict__ to any
extension type by setting the tp_dictoffset member of the type
definition struct to the appropriate offset into the instance struct.)



More information about the Python-list mailing list