Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: =?UTF-8?Q?Thomas_G=c3=bcttler?= Newsgroups: de.comp.lang.python Subject: [Python-de] =?utf-8?q?Syntax-Erweiterung_f=C3=BCr_Schleifen_in_Py?= =?utf-8?q?thon3?= Date: Tue, 5 Apr 2016 20:56:18 +0200 Lines: 59 Message-ID: References: <57040A52.9020404@thomas-guettler.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de RPpPRWXsFNgYxs/bOpjeuQlGfvIdTDzJWU6vO1DaTjbw== Return-Path: X-Original-To: python-de@python.org Delivered-To: python-de@mail.python.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:41.0) Gecko/20100101 Thunderbird/41.0 X-BeenThere: python-de@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Die Deutsche Python Mailingliste List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <57040A52.9020404@thomas-guettler.de> Xref: csiph.com de.comp.lang.python:4349 Hallo, der Thread "Schleifen die ohne Durchlaufen des Schleifenkörpers beendet wurden" war sehr interessant. Während dem Skifahren weit abseits von Laptop und Python viel mir etwas ähnliches ein, dass ich in dieser StackOverflow frage formuliert habe: http://stackoverflow.com/questions/36399808/loop-in-python-do-stuff-before-first-iteration {{{ I want to optimize. # Simple solution connection = get_db_connection() for item in my_iterator: push_item_to_db(item, connection) *Drawback:* `get_db_connection()` is slow. If `my_iterator` is empty, then I want to avoid to call it. # "if None" solution connection = None for item in my_iterator: if connection is None: connection = get_db_connection() push_item_to_db(item, connection) *Drawback:* If there are 100k items in `my_iterator`, then `if connection is None` gets called 100k times (although it is needed only once). I want to avoid this. # Perfect solution ... 1. don't call `get_db_connection()` if iterator is empty 1. don't call `if` uselessly for every iteration. Any idea? }}} Während es im Thread "Schleifen die ohne Durchlaufen des Schleifenkörpers beendet wurden" hauptsächlich darum ging wie man das mit dem aktuellen Python lösen kann, hier die Überlegung: Warum nicht die Syntax in Python3.x erweitern? Bei Schleifen fallen mir diese Anwendungsfälle ein: - on-empty - pre-first (siehe oben) - on-break (wie bisher "else") Gruß, Thomas -- http://www.thomas-guettler.de/