From 1a7bf3c7ea1874d15bb090000e0a60c1ed5d4770 Mon Sep 17 00:00:00 2001 From: Keith Philpott Date: Thu, 11 Jan 2024 22:33:36 -0800 Subject: [PATCH 1/2] replace appends with list comprehensions for speed --- Lib/dataclasses.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 2fba32b5ffbc1e..634f7b9c376afe 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -591,13 +591,13 @@ def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init, '__dataclass_builtins_object__': object, }) - body_lines = [] - for f in fields: - line = _field_init(f, frozen, locals, self_name, slots) + body_lines = [ + line + for f in fields # line is None means that this field doesn't require # initialization (it's a pseudo-field). Just skip it. - if line: - body_lines.append(line) + if (line := _field_init(f, frozen, locals, self_name, slots)) + ] # Does this class have a post-init function? if has_post_init: @@ -1342,11 +1342,10 @@ def _asdict_inner(obj, dict_factory): for f in fields(obj) } else: - result = [] - for f in fields(obj): - value = _asdict_inner(getattr(obj, f.name), dict_factory) - result.append((f.name, value)) - return dict_factory(result) + return dict_factory([ + (f.name, _asdict_inner(getattr(obj, f.name), dict_factory)) + for f in fields(obj) + ]) elif isinstance(obj, tuple) and hasattr(obj, '_fields'): # obj is a namedtuple. Recurse into it, but the returned # object is another namedtuple of the same type. This is @@ -1416,11 +1415,10 @@ def _astuple_inner(obj, tuple_factory): if type(obj) in _ATOMIC_TYPES: return obj elif _is_dataclass_instance(obj): - result = [] - for f in fields(obj): - value = _astuple_inner(getattr(obj, f.name), tuple_factory) - result.append(value) - return tuple_factory(result) + return tuple_factory([ + _astuple_inner(getattr(obj, f.name), tuple_factory) + for f in fields(obj) + ]) elif isinstance(obj, tuple) and hasattr(obj, '_fields'): # obj is a namedtuple. Recurse into it, but the returned # object is another namedtuple of the same type. This is From ffc6e603b15dbc563e21a16ba60a668e0ed88946 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:22:56 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-01-13-00-22-55.gh-issue-114011.Om2h3p.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-01-13-00-22-55.gh-issue-114011.Om2h3p.rst diff --git a/Misc/NEWS.d/next/Library/2024-01-13-00-22-55.gh-issue-114011.Om2h3p.rst b/Misc/NEWS.d/next/Library/2024-01-13-00-22-55.gh-issue-114011.Om2h3p.rst new file mode 100644 index 00000000000000..e9bc75445f0ab2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-01-13-00-22-55.gh-issue-114011.Om2h3p.rst @@ -0,0 +1 @@ +Convert several for loops in the dataclasses module to list comprehensions.