I’ve been working on my custom package (Crumple) lately, and wanted to garner people’s opinions/expectations RE node input/output behavior regarding list structure and objects.
As many will know, in Python you have to force data into a list in order to iterate over it (do one process to each thing in a list), even if dealing with just one object. The most common way I and others seem to deal with this is to use a function to force an item into a list on its own if the node is only given one object, then the functions iterate once over it.
Currently I return this outcome as a list (of one item). My thinking here is that this will let developers also have scripts which can assume the output of said node will always be a list, and be indexable and workable as a list in Dynamo once it leave the custom node. Anyone that has looked into Crumple nodes will probably recognize my list helper functions:
# Define list/unwrap list functions
def tolist(input):
result = input if isinstance(input, list) else [input]
return result
def uwlist(input):
result = input if isinstance(input, list) else [input]
return UnwrapElement(result)
I’ve been toying with the idea of checking the outputs of my node and instead returning an item (where relevant) versus a list if the user only provided an item originally, using a simple length check where if length = 1, return list[0] instead of list.
On the one hand this might make my nodes behave more as users would expect them to (and more akin to the core Dynamo nodes themselves) if they didn’t know how iteration works behind the scenes, but on the other hand I worry that this might just be bad practice as on average some scripts might be giving an item in some cases, and lists in others, so predictable output structure is superior. I do have a node in my package to force list promotion (List.Force), but it feels a pain to force users to engage with an extra node each time they get an output if so.
I’m aware there are some pretty complex ways (recursive map/lambda/functions such as this) to make functions work across unpredictable list structures, but in the interest of keeping my nodes simple/approachable I would prefer not to delve into that space. My core goal with my package was always to lay down each node as an educational piece, so if I go down this route I suspect that goal would be lost. I appreciate the package fulfils more utility than that for most of its users.
I suspect I’ve taken the right path of forcing list output for objects, but wanted to get some insight from others who have likely faced this before or have more advanced development experience than myself (a lot of people here!).
Any thoughts from others on the forums on this topic? I’m sticking with list outputs for now, so if you use my package have no fear!