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


Groups > de.comp.lang.python > #4349

[Python-de] Syntax-Erweiterung für Schleifen in Python3

From Thomas Güttler <guettliml@thomas-guettler.de>
Newsgroups de.comp.lang.python
Subject [Python-de] Syntax-Erweiterung für Schleifen in Python3
Date 2016-04-05 20:56 +0200
Message-ID <mailman.2.1459952729.1197.python-de@python.org> (permalink)
References <57040A52.9020404@thomas-guettler.de>

Show all headers | View raw


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/

Back to de.comp.lang.python | Previous | Next | Find similar


Thread

[Python-de] Syntax-Erweiterung für Schleifen in Python3 Thomas Güttler <guettliml@thomas-guettler.de> - 2016-04-05 20:56 +0200

csiph-web