I’m having a huge problem with python embed in rhino. I’m working with a dictionary, trying to run a loop on it but python 2.7 shows a non-sequencial values of the dictionary when I run this loop. It happens when I print all the dictionary, too. The sorted() function does not work in this case because I’m using numbers (floats,int) as strings. And when I sort it the number 10 comes before the number 4, for example.
I tested it in python 3.9, and it works as expected, the dictionary is shown as it’s written, the loop runs ok, the positions are all correct. I also tested it in python 2.7 outside rhino and it runs like IronPython IDE.
It is a python 2.7 issue.
Please, update python in rhino on the next versions, help us make better scripts.
On this job I will have to make an unconvinient workaround to run it as expected.
Use an ordered dictionary, instead of the regular one. It preserves the order in which the keys are inserted. The standard dictionary is unordered and that is not different in Python 3!
from collections import OrderedDict
od = OrderedDict()
od["a"] = "apples"
od["b"] = "oranges"
od["c"] = "pears"
print od
You should also note that Rhino ships with IronPython, not CPython!
This changed unofficially in Python 3,6 and officially in 3.7 - They found a way to make dictionaries quicker which incidentally preserved insertion order. They liked it so much they decided to keep it as part of the language!
Dictionnaires are really important in Python as every object and every name space keeps all its variables and methods in a dictionary so they are key to the performance of the whole language
Yes this would be great. There have been multiple postings from Rhino developers talking about possibly adding a Cpython interpreter without removing the Ironpython one, but with no definite plans, timescales or target Rhino version number.
Seeing the efforts that are poured into coming up with creative workarounds, like for instance Hops, doesn’t render me too optimistic this will ever happen.