@@ -59,7 +59,7 @@ func init() {
5959 py .MustNewMethod ("round" , builtin_round , 0 , round_doc ),
6060 py .MustNewMethod ("setattr" , builtin_setattr , 0 , setattr_doc ),
6161 // py.MustNewMethod("sorted", builtin_sorted, 0, sorted_doc),
62- // py.MustNewMethod("sum", builtin_sum, 0, sum_doc),
62+ py .MustNewMethod ("sum" , builtin_sum , 0 , sum_doc ),
6363 // py.MustNewMethod("vars", builtin_vars, 0, vars_doc),
6464 }
6565 globals := py.StringDict {
@@ -709,3 +709,51 @@ Update and return a dictionary containing the current scope's local variables.`
709709const globals_doc = `globals() -> dictionary
710710
711711Return the dictionary containing the current scope's global variables.`
712+
713+ const sum_doc = `sum($module, iterable, start=0, /)
714+ --
715+ Return the sum of a \'start\' value (default: 0) plus an iterable of numbers
716+
717+ When the iterable is empty, return the start value.
718+ This function is intended specifically for use with numeric values and may
719+ reject non-numeric types.
720+ `
721+
722+ func builtin_sum (self py.Object , args py.Tuple ) (py.Object , error ) {
723+ var seq py.Object
724+ var start py.Object
725+ err := py .UnpackTuple (args , nil , "sum" , 1 , 2 , & seq , & start )
726+ if err != nil {
727+ return nil , err
728+ }
729+ if start == nil {
730+ start = py .Int (0 )
731+ } else {
732+ switch start .(type ) {
733+ case py.Bytes :
734+ return nil , py .ExceptionNewf (py .TypeError , "sum() can't sum bytes [use b''.join(seq) instead]" )
735+ case py.String :
736+ return nil , py .ExceptionNewf (py .TypeError , "sum() can't sum strings [use ''.join(seq) instead]" )
737+ }
738+ }
739+
740+ iter , err := py .Iter (seq )
741+ if err != nil {
742+ return nil , err
743+ }
744+
745+ for {
746+ item , err := py .Next (iter )
747+ if err != nil {
748+ if py .IsException (py .StopIteration , err ) {
749+ break
750+ }
751+ return nil , err
752+ }
753+ start , err = py .Add (start , item )
754+ if err != nil {
755+ return nil , err
756+ }
757+ }
758+ return start , nil
759+ }
0 commit comments