I’m just starting getting into Python – my focus being on using it with Maya and its API – and I’ve found that when I’m working on something there’s, generally, at least 2 or 3 ways to do the same thing that I’m trying to do. For instance:
for key, value in locNameConnector.iteritems():
value = locNameConnector[key]
cmds.connectJoint(value, key, pm=True)
or
for name in locNameConnector:
cmds.connectJoint(locNameConnector[name][0], name, pm=True)
now the code is calling specific things in Maya, but my question is, which way is more correct? I feel like the first one is because it’s taking advantage of Python’s power, while the second one could be written in any language. Is there a more correct way? Is one faster than the other?
One of Python’s philosophies is:
(You can see a list of these – known as The Zen of Python – by doing
import thisin the Python shell.)And Python does try and make it so that there really is only one way. But for any language beyond a very basic level of functionality, though, there is always going to be more than one way to do anything non-trivial – that’s simply the way programming is. But even when there is more than one way, there is usually one that’s regarded as more ‘Pythonic’.
As regards your actual examples, they don’t seem to be equivalent – the first one is looking up
valueinl_LegJointConnectors, the second looks it up inlocNameConnector. Is that second line in the first example supposed to be there? If not, the first one is very definitely more Pythonic than the second.