Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__dict__ attribute of PathExAttMap is always empty #68

Open
afrendeiro opened this issue Aug 13, 2019 · 1 comment
Open

__dict__ attribute of PathExAttMap is always empty #68

afrendeiro opened this issue Aug 13, 2019 · 1 comment

Comments

@afrendeiro
Copy link

afrendeiro commented Aug 13, 2019

A little example on how the objects sometimes behave a bit intriguingly:

>>> d = {"key": "value"}
>>> a = AttMap(d)
>>> [x for x in a]
['key']
>>> a.__dict__
{'key': 'value'}
>>> a = PathExAttMap(d)
>>> [x for x in a]
['key']
>>> a.__dict__
{}

I think having a functional __dict__ on any Python object is pretty standard and would be nice if PathExAttMap and the child peppy.Sample would have it too.

@vreuter
Copy link
Member

vreuter commented Oct 22, 2019

It's because PathExAttMap is ordered. So OrdAttMap also has this behavior, but more general AttMap doesn't, like ordinary dict vs. OrderedDict:

master└─ $ ipython
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from attmap import AttMap as AM, OrdAttMap as OAM, PathExAttMap as PXAM                                                                                                                             

In [2]: from collections import OrderedDict                                                                                                                                                                 

In [3]: am = AM()                                                                                                                                                                                           

In [4]: o = OrderedDict()                                                                                                                                                                                   

In [5]: oam = OAM()                                                                                                                                                                                         

In [6]: pxam = PXAM()                                                                                                                                                                                       

In [7]: ds = [am, o, oam, pxam]                                                                                                                                                                             

In [8]: for d in ds: 
   ...:     d["a"] = 1 
   ...:                                                                                                                                                                                                     

In [9]: [m.items() for d in ds]                                                                                                                                                                             
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-83e8b68a814e> in <module>
----> 1 [m.items() for d in ds]

<ipython-input-9-83e8b68a814e> in <listcomp>(.0)
----> 1 [m.items() for d in ds]

NameError: name 'm' is not defined

In [10]: [d.items() for d in ds]                                                                                                                                                                            
Out[10]: 
[ItemsView(AttMap
 a: 1), odict_items([('a', 1)]), [('a', 1)], [('a', 1)]]

In [11]: [d.__dict__ for d in ds]                                                                                                                                                                           
Out[11]: [{'a': 1}, {}, {}, {}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants