Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Dynamically generating variable names is almost always a bad approach. Use a dictionary!</p> <pre><code>bonedict = {'boneList_head': ['def_neck', 'def_armbase']} itemType='head' def selectBones(itemType): bones = bonedict['boneList_' + itemType] for bone in bones: cmds.select(bone, tgl=True) </code></pre> <p>Please ignore my previous answer (visible in my edit history) which was stupid -- boneheaded, even. But I blame its stupidity on dynamic variable name generation!</p> <p>Let me elaborate on why dynamic variable name generation is a bad idea.</p> <ol> <li><p>Because dynamic variable generation masks variable name definitions. It's hard to tell what has been defined and what hasn't, so it's easy to accidentally redefine a variable. This is a major source of potential bugs. </p></li> <li><p>Because dynamic variable manipulation hides state changes under another layer of obfuscation. To some degree, this is true anytime you create a dictionary or a list. But one <em>expects</em> lists and dictionaries to demand a little extra thinking. Variable names, on the other hand, should be dead simple. When variable definitions and redefinitions require deep thought to understand, something is wrong.</p></li> <li><p>Because dynamic variable generation pollutes the namespace. If you have so many variables that you have to automatically generate them, then they should live in their own namespace, not in the locals of a function, and <em>definitely</em> not in the global namespace. In his style guide for the linux kernel, Linus Torvalds advises that if a function has more than 5-10 local variables, <a href="https://computing.llnl.gov/linux/slurm/coding_style.pdf" rel="nofollow noreferrer">you're doing something wrong</a>.</p></li> <li><p>Because dynamic variable generation contributes to <a href="http://en.wikipedia.org/wiki/Coupling_(computer_programming)" rel="nofollow noreferrer">high coupling</a>, which is a bad thing. If you assign to values to a dictionary, you can pass that dictionary back and forth until the cows come home, and all anyone has to know about is that dictionary. If you dynamically create variable names in the global namespace of a module, then if another module wants to access those variable names, it has to know all about the way they were generated, what other variables in that module are defined, and so on. Also, passing the variables around becomes much more complex -- you have to pass around a reference to the module itself, probably using <code>sys.modules</code> or other questionable constructs.</p></li> <li><p>Because dynamic variable generation is ugly. <code>eval</code> looks neat and clean, but it really isn't. It can do <em>anything</em>. Functions that can do anything are bad, because you can't tell at first glance what they're doing <em>here</em>. A well-defined function does one thing, and does it well; that way, whenever you see that function, you know exactly what's happening. When you see <code>eval</code>, literally <em>anything</em> could be happening. In this sense, <code>eval</code> is like <code>goto</code>. The <a href="https://stackoverflow.com/questions/46586/goto-still-considered-harmful">problem</a> with <code>goto</code> is not that you can't use it correctly; it's that for every possible correct use of <code>goto</code>, there are 500,000,000 terrifyingly wrong ways to use it. I won't even discuss the security problems here, because in the end, that's not the real problem with <code>eval</code>. </p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload