[Python-de] Re: Zuweisung via return

Andreas Röhler <[email protected]> Thu, 4 Jul 2024 13:43:46 +0200
Newsgroups gmane.comp.python.general.german
Message-ID <[email protected]>
Am 03.07.24 um 11:14 schrieb Hartmut Goebel:
> Am 02.07.24 um 20:56 schrieb Andreas Röhler:
>> print("zaehler: {}".format(zaehler))
>
> Immer wieder sehe ich diese unnötigen und ineffizienten
> String-Formatierungen bei "print".
>
> print("zaehler:", zaehler)
>
> tut genau das gleiche.
>

In diesem Fall schon. Aber warum für jede Aufgabe eine neue Form verwenden?

Nach meinem Dafürhalten ist, was "Format Specification Mini-Language"
anbietet, ungleich mächtiger - und am Ende auch einfacher.

Hab mal ein paar Beispiele herausgesucht:


# python/python-3.10.13-docs-html/library/string.html#formatspec

# Accessing arguments by position:
print('{0}{1}{0}'.format('abra', 'cad'))
# >>> >>> abracadabra

# Accessing arguments by name:
print('Coordinates: {latitude}, {longitude}'.format(latitude='37.24N',
longitude='-115.81W'))
# >>> Coordinates: 37.24N, -115.81W

# Replacing %s and %r:
print("repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1',
'test2'))
# >>> repr() shows quotes: 'test1'; str() doesn't: test2

# Accessing arguments’ attributes:
c = 3-5j
print(('The complex number {0} is formed from the real part {0.real} '
'and the imaginary part {0.imag}.').format(c))
# >>> The complex number (3-5j) is formed from the real part 3.0 and the
imaginary part -5.0.

# Using type-specific formatting:
import datetime
d = datetime.datetime(2010, 7, 4, 12, 15, 58)
print('{:%Y-%m-%d %H:%M:%S}'.format(d))
# >>> 2010-07-04 12:15:58

_______________________________________________
python-de Mailingliste -- [email protected]
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected]
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: [email protected]