Changes between Version 73 and Version 74 of DeveloperGuidelines/Py_2_3


Ignore:
Timestamp:
07/10/19 22:14:23 (5 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/Py_2_3

    v73 v74  
    300300
    301301- {{{zipfile}}} expects and returns {{{bytes}}}
     302
     303=== Sorting requires type consistency ===
     304
     305When sorting iterables with {{{i.sort()}}} or {{{sorted(i)}}}, all elements must have the same type - otherwise it will raise a {{{TypeError}}} in Python-3.
     306
     307This is particularly relevant when the iterable can contain {{{None}}}. In such a case, use a key function to deal with None:
     308{{{#!python
     309l = [4,2,7,None]
     310
     311# Works in Py2, but raises a TypeError in Py3:
     312l.sort()
     313
     314# Works in both:
     315l.sort(key=lambda item: item if item is not None else -float('inf'))
     316}}}