Jump to content

Comparing None (NIL) handles in Python strange error


_c_

Recommended Posts

I don't understand this.

if I use in an external editor (Visual Studio Code, should that be relevant) this code I get bucketloads of errors:

h = vs.GetCustomObjectPath( gPio_H )
if h is not None:
....

 

 

While the same, expressed with operators, doesn't:

h = vs.GetCustomObjectPath( gPio_H )
if h != None:
....

But only in the external editor. In a doc script both forms possible without any problem.

What could cause this?

 

Please read the Python Programming recommendations:

www.python.org/dev/peps/pep-0008/#programming-recommendations

 

 

Quote

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

 

 

Link to comment

No, it is definitely weird. I do have it also in the normal VS editor.

 

This causes errors:

import vs
def LinkIt(obj):
    recH = vs.GetParametricRecord(obj)
    
    if recH is not None: # uses words
        vs.AlrtDialog(vs.GetName(recH))
        
def Sub(pt):
    vs.ForEachObjectAtPoint(LinkIt, 0, 0, pt[0], pt[1], 5)
    
h = vs.FSActLayer
vs.GetPt( Sub )

 

While this does not:

import vs

def LinkIt(obj):
	recH = vs.GetParametricRecord(obj)
	
	if recH != None: # uses operators
		vs.AlrtDialog(vs.GetName(recH))

def Sub(pt):
	vs.ForEachObjectAtPoint(LinkIt, 0, 0, pt[0], pt[1], 5)

h = vs.FSActLayer
vs.GetPt( Sub )

 

But this never gives problems:

 

import vs

def test(h):
	if h is not None: # uses words
		vs.AlrtDialog('not NIL')

h = vs.FSActLayer
test(h)

1115646917_Screenshot2021-01-24at12_48_14.thumb.png.329a2356a6cd131bb0f95ea6b18ef46c.png

Edited by _c_
Link to comment

I believe the issue is that the handle is actually a class and not a value, so that indeed it’s not None (the variable is assigned to an object), it’s that the object evaluates to nil. So your line 6 is actually succeeding when it shouldn’t and line 7 executes with a null pointer. I suspect that the internal/external editor difference is a red herring — you were just detecting an invalid object. 
 

The test I use is  != vs.Handle(0), that is, a handle that points to nothing, which is different from a non-existent handle. 

  • Like 1
Link to comment

It has been said before and it is perfectly useless to say it again, but indeed variable declaration and handling, while less powerful, is clearly a no-brainer in Vectorscript Pascal, while in Python it is a major source of headaches. I can't begin even speaking of how much I miss Vector variables in Pascal. And yes, I am using vector classes in Python.

 

Rant off. I'll get fit in this fast enough.

 

Edited by _c_
Link to comment
  • 1 year later...

did you find any conclusion?

actually I tried the following code, understanding that it won't work.

However, I want to check wether the worksheet already exists before I create it.

if worksheet = nil: however doesn't work with a handle, obviously (and "NIL" doesn't exist in Python)

What can I do?

switch back to Vectorscript? 😞  

	worksheet = vs.GetObject('WS_TEST')
    if worksheet = nil:
		vs.CreateWS(('WS_TEST'),2,2)

 

Link to comment

is there a nil object in python?

In any case, for handle error checking I normally use:

# based on your example 
worksheet = vs.GetObject('WS_TEST')
if worksheet not in [None, 0]:
    vs.CreateWS(('WS_TEST'), 2,2)

 

Link to comment

😞 that didn't help

I just want to prevent, that the worksheet already exists before I create a new one 😉 

for a in range (4):
#	vs.CreateWS('WS_TEST'+a+',2,2')
	worksheet = vs.GetObject(vs.Concat('WS_TEST',a))
    if worksheet not in [None, 0]:
		vs.CreateWS((vs.Concat('WS_TEST',a)),2,2)
	vs.SetWSColumnWidth((vs.Concat('WS_TEST',a)),1,1,50)
	vs.SetWSColumnWidth((vs.Concat('WS_TEST',a)),1,1,20)
	vs.SetWSRowHeight((vs.Concat('WS_TEST',a)),1,1,20,0,0)
	vs.SetWSRowHeight((vs.Concat('WS_TEST',a)),2,2,20,0,0)
	vs.SetWSCellFormulaN((vs.Concat('WS_TEST',a)),1,1,1,1,a)

1496833676_Bildschirmfoto2022-02-20um23_08_18.thumb.png.9afad571f0355e24da3632ae2855017b.png

 

 

create-ws.zip

Edited by matteoluigi
Link to comment

The error is saying your indentations are inconsistent/incorrect.

Indentations are important for python. You must either use 4-spaces, or a single TAB, you can't use both in one script.

try this:

for a in range (4):
#	vs.CreateWS('WS_TEST'+a+',2,2')
	worksheet = vs.GetObject(vs.Concat('WS_TEST',a))
	if worksheet not in [None, 0]:
		vs.CreateWS((vs.Concat('WS_TEST',a)),2,2)
	vs.SetWSColumnWidth((vs.Concat('WS_TEST',a)),1,1,50)
	vs.SetWSColumnWidth((vs.Concat('WS_TEST',a)),1,1,20)
	vs.SetWSRowHeight((vs.Concat('WS_TEST',a)),1,1,20,0,0)
	vs.SetWSRowHeight((vs.Concat('WS_TEST',a)),2,2,20,0,0)
	vs.SetWSCellFormulaN((vs.Concat('WS_TEST',a)),1,1,1,1,a)

 

Edited by twk
  • Like 2
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...