Skip to content

Commit d16f9f2

Browse files
committed
Added Expando.copy()
1 parent e11961d commit d16f9f2

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/bd2k/util/expando.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,43 @@ class Expando(dict):
5757
...
5858
AttributeError: foo
5959
60+
And copied:
61+
62+
>>> o = Expando(foo=42)
63+
>>> p = o.copy()
64+
>>> isinstance(p,Expando)
65+
True
66+
>>> o == p
67+
True
68+
>>> o is p
69+
False
70+
71+
Same with MagicExpando ...
72+
73+
>>> o = MagicExpando()
74+
>>> o.foo.bar = 42
75+
>>> p = o.copy()
76+
>>> isinstance(p,MagicExpando)
77+
True
78+
>>> o == p
79+
True
80+
>>> o is p
81+
False
82+
83+
... but the copy is shallow:
84+
85+
>>> o.foo is p.foo
86+
True
6087
"""
6188

6289
def __init__( self, *args, **kwargs ):
6390
super( Expando, self ).__init__( *args, **kwargs )
6491
self.__slots__ = None
6592
self.__dict__ = self
6693

94+
def copy(self):
95+
return type(self)(self)
96+
6797
class MagicExpando(Expando):
6898
"""
6999
Use MagicExpando for chained attribute access. The first time a missing attribute is

0 commit comments

Comments
 (0)