Python for..in range in custom node error

Hi there,
I created a python script to get all the divisors of a number and it works fine but as soon as I create a custom node with it the code fails.
I tried several other functions in the last few days and I understood this is related to the for x in range loop.
Outside the custom node it throws no error but inside the custom node the output is null.
The same code with a while loop works fine both in python node and in custom node.
Do you have any idea why?

Code NOT working in custom node

myNum = IN[0]
divisors = []

for i in range(1,myNum+1):
	if myNum % i == 0:
		divisors.append(i)

Code working in custom node

myNum = IN[0]
divisors = []
TmpNum = myNum

while TmpNum >= 1:
	if myNum % TmpNum == 0:
		divisors.append(TmpNum)
	TmpNum = TmpNum - 1

divisors.reverse()

See attached image with code results

Thanks in advance for your kind reply.

Likely because once moved to a custom node your data is being passed as a single item. Set your input to be a list at the input node (not in the python node): myNum : var[].

Hi @jacob.small,
thank you for your reply.
I tried but nothing changes.
See image below of my custom node with for loop
CustomNode_ForLoopNullErrorVar

Still I don’t get why the custom nodes with for loop and while loop act differently.

Hello @paolo.pozzoli - You’ll need to use the arbitrary ranking in your inputs :slight_smile: You can learn more about it in the DesignScript Language Guide under clause 3.3.1.1 Rank, where A “[]..[]” specifies arbitrary rank, which means that the list can be of any rank including a single value or rank 0.

This is also a good default ranking to use as the others (None, Rank 1 :[ ], and Rank 2 :[ ][ ]) can constrain your custom node… useful for sure in the right context, but best to start with arbitrary ranking :slight_smile:

Inputs:

number: var []..[]

Result:
image

4 Likes

Good catch @solamour!

1 Like

Hi @solamour, great one thank you for pointing me in the right direction!

Just one question aside from this specific matter:
var can accept any type value (int,double, bool, str) just as in VB variant type?
If so wouldn’t be better to set an input for the right type (I mean bool, int, double, str) instead of using var? Otherwise a conditional check or try/catch in tge python code would be needed in my point of view.
Does it make sense to you my thought?

Thanks again
Bye

@paolo.pozzoli indeed - if you only ever want numbers coming in here (As you have), then you can choose to have either Int or Double inputs - these will automatically throw out any data type that isn’t matched and is thus more efficient :slight_smile:

And yes, Var = Variable (So any type) same as VB.

2 Likes

By the time I wrote I figured it out myself by reading DS 3.3.1.1:

int … works as well
So I guess also double … works too in my case :slightly_smiling_face:

1 Like

It will work yes! :slight_smile: Rank is separate to Object Type in the Input syntax of custom nodes.

1 Like

Thanks again for your time Sol :wink:

2 Likes

You are most welcome! :raised_hands:

1 Like