[Python-de] strings zusammensetzen.

Thomas Orgelmacher trash at odbs.org
Di Aug 29 13:05:44 EDT 2017


Am 25.08.2017 um 07:45 schrieb Hermann Riemann:
> Was ist besser?
> a b und c enthalten strings.
> 
> d=a+b+c besser als
> d="{}{}{}".format(a,b,c) ?

In diesem Fall ist das fast egal. Auch die Performance wird nahezu
identisch sein. Ich würde den + Operator bevorzugen.

Was man nicht machen sollte ist eine Konstruktion wie

s = ''
for i in range(1000):
     s = s + 'x'

Hier wird 1001 mal ein str instantiiert und 1000 müssen vom CG wieder
entsorgt werden.

Da wäre ein ''.join(['x' for i in range(1000)]) deutlich effektiver.
Ja, ich weiß. 'x' * 1000 wäre noch besser, aber es ging ja nur ums
Beispiel.


Thomas

-- 
I have seen things you lusers would not believe.  I've seen Sun
monitors on fire off the side of the multimedia lab. I've seen
NTU lights glitter in the dark near the Mail Gate. All these
things will be lost in time, like the root partition last week.



Mehr Informationen über die Mailingliste python-de