Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #196246

Suggested python feature: allowing except in context maneger

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Yair Eshel <guruyaya@gmail.com>
Newsgroups comp.lang.python
Subject Suggested python feature: allowing except in context maneger
Date Thu, 13 Jun 2024 13:01:11 +0300
Lines 52
Message-ID <mailman.130.1718284531.2909.python-list@python.org> (permalink)
References <CAAtpb8kxWfLVKYW3w3uJRyyQi-3FPxZHYfS2BLe0R6UYSitgZw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de 2+3kTWoeJN2xQ898G24LWgUxDmvJDLzmHFEB1o27u3pw==
Cancel-Lock sha1:egXQ5NuykNTe/AJx8WLGSnMbfZ8= sha256:rta4307ApTiXtOqUIkKXedRFgKwq7y1woEIPbcTA2ho=
Return-Path <guruyaya@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
Authentication-Results mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=gmail.com header.i=@gmail.com header.b=QIH+DT5x; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status UNSURE 0.266
X-Spam-Level **
X-Spam-Evidence '*H*': 0.53; '*S*': 0.06; 'usage': 0.05; 'subject:python': 0.06; 'python.': 0.08; 'readable': 0.09; 'import': 0.15; 'applies': 0.16; 'syntax,': 0.16; 'syntax.': 0.16; 'url:#': 0.16; 'work:': 0.16; 'code.': 0.17; 'to:addr:python- list': 0.20; 'code': 0.23; 'idea': 0.24; 'behavior': 0.26; 'manager,': 0.26; 'normally': 0.26; 'suggest': 0.28; 'suggestions': 0.28; 'context': 0.32; 'message- id:@mail.gmail.com': 0.32; "i'm": 0.33; 'there': 0.33; 'received:google.com': 0.34; 'following': 0.35; 'from:addr:gmail.com': 0.35; 'this.': 0.37; 'way': 0.38; 'could': 0.38; 'read': 0.38; 'handle': 0.39; 'use': 0.39; 'both': 0.40; 'provide': 0.60; 'best': 0.61; 'skip:o 20': 0.63; '8bit%:9': 0.64; 'less': 0.65; 'let': 0.66; 'url:net': 0.68; 'know.': 0.68; 'everyday': 0.69; 'natural': 0.69; 'url-ip:134.209/16': 0.69; 'url-ip:134/8': 0.69; 'future': 0.72; 'features': 0.75; 'skip:f 20': 0.75; '8bit%:100': 0.76; '8bit%:95': 0.84; 'cleaner': 0.84; 'hopes': 0.91; 'url:start': 0.91; 'will,': 0.91; 'manager.': 0.93; 'pain': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718272883; x=1718877683; darn=python.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=cRV+ggLf6hDdueYM3r2GnCmHu5xeziYri7k0tNVLvRY=; b=QIH+DT5xdIyEI03+yQNCXPAimViXzMDGmemPFz6T1DQyaNgqLSPSwIFleMxwUrWUXw w3ZblQSat2rTARE4lCdThSAXJfVoHjx3BK7RfHFYNG5IeCfsY7+ywR0OBvOX9U+i+7Xp sIvgIw6hd9/kOT1tmReGYCbqymilTPQdkxHm07nnxtk4uBGrWUnypUI4yhZ0OpiTsdZ+ TcXJ/lB6z8lJpN14pr98lkUoX6sIZQ4YHZ+1R/lcL+2vcnT0F+55uhGnlhElvfjclu5x 7TbqiCpiHWNiSCIrxiwA9uD5AdoRb4zICPALe3ff08iyH2VKtfcJicoeEjS05jw2BvZ+ 5efw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718272883; x=1718877683; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=cRV+ggLf6hDdueYM3r2GnCmHu5xeziYri7k0tNVLvRY=; b=Ci+2TEzG8LxOoUIKyQuKupdXCGyHFFYToq4PKypuUH6rzsTjRz7mayWzpgH7LcFsYv uAvnrG/YWKzIP+rVCXP+S+hNhgxR+IWk1cbb1Hy3NZAfsJLYRotUhAXOdrb3TeFJzHcf JVYnh8v51hxx97G4B9Q9SHF+YdSvf6a1fSKEKyVf3rUthQO0XSjkaraxkUP9lOV2IeJo yMvllht4XLN8mb4FAw41GHaQbGNo+QweHDr1JeMG0wrrlqhdlLSFDoM7Lx6/TA1EG/Ak GKclGQtFzez02SKFTNq4RYpVKmJOAobztkcVHU1AUdaGGDTtIVeWb0JuWimkjeXqr0jq rWtg==
X-Gm-Message-State AOJu0YzpJdX5f5ROelJ7V/U/mGrQ42xV7NRYP8QI9/DyU7O8+BcJXKAW COwjEBBroz6QPjwKnq1lDpxJBYrESdX4I3Ip98IEpTi00hge29Jt3kjwjCRiV94Y6mYHem5oqGx dnyT9Y1WIo2ZsG7YewU9DKjLrsP0xh/S2
X-Google-Smtp-Source AGHT+IHX+Nzez12clkx8pa2RiN8H7JG9RLMPzk1dGSA23NEbwJ/ZHsO0CZhrDdAZa7jWlXAnMPgMdJSg2TtFqwazcos=
X-Received by 2002:a05:6870:5687:b0:254:6e4e:23d7 with SMTP id 586e51a60fabf-25514e74ffemr4913004fac.50.1718272882564; Thu, 13 Jun 2024 03:01:22 -0700 (PDT)
X-Mailman-Approved-At Thu, 13 Jun 2024 09:15:31 -0400
X-Content-Filtered-By Mailman/MimeDel 2.1.39
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.39
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <https://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAAtpb8kxWfLVKYW3w3uJRyyQi-3FPxZHYfS2BLe0R6UYSitgZw@mail.gmail.com>
Xref csiph.com comp.lang.python:196246

Show key headers only | View raw


Hello. I read this is a good place to give some suggestions for features in
python. If not, please let me know.

This is an example of a code I normally use in my everyday work:
import logging
try:
  with open('sample_data/READM.md') as f:
    print (len(f.read()))
except FileNotFoundError:
  logging.error("File not found")


As you can see I have 2 levels of indentation, which can add some pain to
the work with the context manager. This code without context manager, can
be replaced by this code:

import logging
try:
  f = open('sample_data/READM.md') as f:
  print (len(f.read()))
except FileNotFoundError:
  logging.error("File not found")
finally:
  f.close()

And while this offers less indentations, it skips the usage of the very
handy context manager.

I would like to suggest an alternative syntax, that will, in a sense, apply
the best of both worlds:

import logging
with open('sample_data/README.md') as f:
  print (len(f.read()))
except FileNotFoundError:
  logging.error("File not")

As "with" applies the behavior of the "try / finally" it feels like a
natural part of this syntax. This could provide a cleaner code.
If this idea is accepted, there are several things that need to be
discussed, like what to do with "else" or "finally" statement following a
context manager. I'm not sure about the proper way to handle this.

With hopes for an even more readable future
Yair
-- 
בברכה,
יאיר אשל כהנסקי
מתכנת וטכנאי מילים
https://www.inspect-element.net/YouAreHere/#/start

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Suggested python feature: allowing except in context maneger Yair Eshel <guruyaya@gmail.com> - 2024-06-13 13:01 +0300

csiph-web