We have a Visual Basic script that communicates with an application through com. We like to translate it to Python and use win32com.client instead. We have everything working, except for the use of "Nothing" in the VB script for a couple of its function calls. What is the replacement of "Nothing" in Python? Note that the generic "None" does not work.

Thanks in advance,
Artman

Yeah, that's interesting ... None in Python is a special type, whereas Nothing is equivalent to NULL of whatever datatype one is working with: Dim b as Integer: b = Nothing -- this makes b a Nothing value of type Integer (as opposed to a None type).

In short, Nothing is a value whereas None is a type.

What you may have to do is sacrifice the flexibility of "Nothing" and manually set the variables to the corresponding value. So for example:

Dim a as Integer
a = Nothing

is equivalent to

Dim a as Integer
a = 0

It's not ideal; perhaps there's a way for your Python script to detect the type of the variable and set it to the corresponding Nothing value using a dictionary:

NothingTypes = {"Integer": 0, "Boolean": False, ...}

...

MyValue = NothingTypes[TypeToString(MyVar)]

That's somewhat pseudo-cody, but I hope it's clear.

Jeff

Good explanation, Jeff.

I don't really have a good handle on Python's type() function, but you can use it as ff:

>>> str(type(56))
"<type 'int'>"
>>> str(type('ABC'))
"<type 'str'>"
>>> str(type(False))
"<type 'bool'>"
>>>

Perhaps others can come up with a better way, but you'd basically do something like:

NothingTypes = {"<type 'int'>": 0, "<type 'str'>":"", "<type 'bool'>": False} # etc
...
MyValue = NothingTypes[type(MyVariable)]

The problem is that, Nothing is used instead of a non-builtin datatype, i,e, an object of a new class and it has no default value for NULL such as 0 for int and etc.

It looks like Nothing is used to declare a variable for use at a later time. Since you don't have to declare variables in Python, you should be able to just ignore it. If you want a variable to be persistent, then you will have to define it with some value. A snippet of the VB code would be helpful. It could then explain why someone is declaring a "non-variable" and what definition they assign to it later. Finally, think in terms of a solution, not a line by line translation of VB code.

It looks like Nothing is used to declare a variable for use at a later time.

Almost. In dynamically typed languages like Python, assigning a value and declaring the type occur in the same motion:

MyList = [1,2,3]

means

"Declare MyList as a reference to a list, and assign it to the list [1,2,3]."

But in statically typed languages -- C, VisualBasic -- variables have to be declared at compile time before being assigned. Thus:

Dim a as Integer  'declares a

a = Nothing 'assigns a

So modify your statement slightly ... "It looks like Nothing is used to assign values for use at a later time."

But put that way, it becomes clear that, even in Python, unassigned variables could throw errors later down in the code. For example:

Dim a as Integer
a = Nothing

GetPossibleNewValue(a) 'might change a, might not
print a

If the assignment is skipped in the corresponding Python code, an error will be thrown at print a.

Jeff

If Nothing is equivalent to NULL, why not use ctypes and get it? BTW I left VB long ago at early learning stage because I wasn't able to buy the VB 6 bundle, so I don't remember it!

I have to pass a variable such as Nothing in VB to a com function. The com function is from the api of an application program and thus unalterable. I should pass a variable such as None instead of Nothing (which is used instead of a non-builtin type). What can I do?

The type of the argument is a non-builtin class and I cannot send the appropriate nothig value to it. It issues the following error when I pass any other value such as pythoncom.Empty instead of VB Nothing to AccessSelection() function:

File "<COMObject <unknown>>", line 2, in AccessSelections
pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 2)

What do you think I should do?

Hm. What happens if you send it an object of the required class? And what is the required class, anyways?

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.