.format() rather than concatenation

42 views
Skip to first unread message

s...@weacceptyou.com

unread,
May 23, 2016, 4:09:10 PM5/23/16
to Python Programming for Autodesk Maya
Hi just a quick one.

Why is it necessary to use .format():

for light in all_lights:
light_intensity = cmds.getAttr('{0}.intensity'.format(light))

rather than:

for light in all_lights:
light_intensity = cmds.getAttr(light + '.intensity')

they both work but i always see people using the .format() method, rather than just concatenating the strings. Is there a reason to use one rather than the other. The latter seems simpler also

thanks,
Sam

Justin Israel

unread,
May 23, 2016, 4:28:18 PM5/23/16
to Python Programming for Autodesk Maya
Concatenating strings creates maple copies along the way, as each pair is concatenated and a new string is return for for the next addition. In small use cases it won't matter. But in loops and with tons of string it would be wasteful. 

Another reason is that using the + operator only works if both sides are strings. Whereas the format() method gives you string conversion operations like float formatting. 


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/d32cb68b-135f-443a-923a-83d7ae1f3034%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
May 23, 2016, 4:29:02 PM5/23/16
to Python Programming for Autodesk Maya


On Tue, 24 May 2016 8:28 AM Justin Israel <justin...@gmail.com> wrote:


On Tue, 24 May 2016 8:09 AM <s...@weacceptyou.com> wrote:
Hi just a quick one.

Why is it necessary to use .format():

for light in all_lights:
    light_intensity = cmds.getAttr('{0}.intensity'.format(light))

rather than:

for light in all_lights:
    light_intensity = cmds.getAttr(light + '.intensity')

they both work but i always see people using the .format() method, rather than just concatenating the strings. Is there a reason to use one rather than the other. The latter seems simpler also

thanks,
Sam

Concatenating strings creates maple copies

multiple 

s...@weacceptyou.com

unread,
May 23, 2016, 4:48:28 PM5/23/16
to Python Programming for Autodesk Maya
ok i see, thanks Justin,

so the extra stuff generated by code in a script, like strings, how can they be problematic or wasteful?, do you mean they just use up memory or slow things down? I dont think i fully understand where the stuff that is created is stored, and does it only dissappear when you restart maya?

thanks,
Sam

Marcus Ottosson

unread,
May 23, 2016, 5:15:25 PM5/23/16
to python_in...@googlegroups.com

Concatenating strings creates maple copies along the way, as each pair is concatenated and a new string is return for for the next addition.

This is probably true, but come on. For performance?

This..

for light in all_lights:
    light_intensity = cmds.getAttr(light + '.intensity')

..is a lot more readable than this..

for light in all_lights:
    light_intensity = cmds.getAttr('{0}.intensity'.format(light))

And that makes it right.

Justin Israel

unread,
May 23, 2016, 5:20:18 PM5/23/16
to python_in...@googlegroups.com
On Tue, May 24, 2016 at 8:48 AM <s...@weacceptyou.com> wrote:
ok i see, thanks Justin,

so the extra stuff generated by code in a script, like strings, how can they be problematic or wasteful?, do you mean they just use up memory or slow things down? I dont think i fully understand where the stuff that is created is stored, and does it only dissappear when you restart maya?

Think of it like this:

a + b + c + d

tmp1 = a+b
tmp2 = tmp1 + c
tmp3 = tmp2 + d

Python has to allocate memory for a bunch of temporary strings as each pair is concatenated. Then the garbage collector will reap them. That can mean both wasted memory and slow performance. Consider if a,b,c,d were very large strings. It would mean you would have to create large temporary intermediate strings that get thrown away.

Now the behaviour of string concatenation using the + operator is implementation specific, most likely. Meaning, it is up to the particular python interpreter to decide how to evaluate it. It could be possible for another type of python interpreter (or another version of the CPython interpreter) to optimize the expression. 

thanks,
Sam


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Justin Israel

unread,
May 23, 2016, 5:22:36 PM5/23/16
to python_in...@googlegroups.com
Surprisingly, you stopped quoting me just before this bit:

"In small use cases it won't matter. But in loops and with tons of string it would be wasteful. "

I specifically said that in small cases it doesn't matter. I was explaining why there is a difference at in, given potential cases.

Readability counts. So, yes, do what is more readable and profile you code to find out if you have hot spots...and don't take things out of context.
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

s...@weacceptyou.com

unread,
May 23, 2016, 7:27:16 PM5/23/16
to Python Programming for Autodesk Maya
thanks alot. will try and get into the habit of using it
Reply all
Reply to author
Forward
0 new messages