File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -1277,3 +1277,9 @@ def translate(self, *args):
1277
1277
return self .__class__ (self .data .translate (* args ))
1278
1278
def upper (self ): return self .__class__ (self .data .upper ())
1279
1279
def zfill (self , width ): return self .__class__ (self .data .zfill (width ))
1280
+
1281
+ # FIXME: try to implement defaultdict in collections.rs rather than in Python
1282
+ # I (coolreader18) couldn't figure out some class stuff with __new__ and
1283
+ # __init__ and __missing__ and subclassing built-in types from Rust, so I went
1284
+ # with this instead.
1285
+ from ._defaultdict import *
Original file line number Diff line number Diff line change
1
+ class defaultdict (dict ):
2
+ def __new__ (cls , * args , ** kwargs ):
3
+ if len (args ) >= 1 :
4
+ default_factory = args [0 ]
5
+ args = args [1 :]
6
+ else :
7
+ default_factory = None
8
+ self = dict .__new__ (cls , * args , ** kwargs )
9
+ self .default_factory = default_factory
10
+ return self
11
+
12
+ def __missing__ (self , key ):
13
+ if self .default_factory :
14
+ return self .default_factory ()
15
+ else :
16
+ raise KeyError (key )
17
+
18
+ def __repr__ (self ):
19
+ return f"defaultdict({ self .default_factory } , { dict .__repr__ (self )} )"
20
+
You can’t perform that action at this time.
0 commit comments