Weird behavior on __dict__ attr

Dave Angel davea at davea.name
Mon Feb 9 22:12:01 EST 2015


On 02/09/2015 09:52 PM, Shiyao Ma wrote:
> Hi.
>
> My context is a little hard to reproduce.

WHY don't you try?  Telling us about a class without showing how it's 
defined leaves us all guessing.

Start by telling us Python version.  And if it's 2.x, tell us whether 
this class is an old style or new style class.

>
> NS3 is a network simulation tool written in C++. I am using its Python binding.
>
> So the class I am dealing with is from a .so file.
>
> Say, I do the following:
>
> %
>
> import ns.network.Node as Node
>
> # Node is a class
> # it has a __dict__ attr
>
> # Now I instantiate an instance of Node
> n = Node()
>
> # I checked, there is no __dict__ on 'n'
> # but the following succeeds.
>
> n.foobar = 3
>
>
>
> My understanding is the foobar is stored in n.__dict__, but seemingly n has no __dict__.
>
> So where does the foobar go?
>

Lots of possibilities.  Simplest is slots.  if you define __slots__, 
then there's no dictionary in each instance.

https://docs.python.org/3.4/reference/datamodel.html#slots

-- 
DaveA



More information about the Python-list mailing list