| | |
'None' in Python and 'Nothing' in VB
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 4
Reputation:
Solved Threads: 0
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
Thanks in advance,
Artman
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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:
is equivalent to
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:
That's somewhat pseudo-cody, but I hope it's clear.
Jeff
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:
VB Syntax (Toggle Plain Text)
Dim a as Integer a = Nothing
is equivalent to
VB Syntax (Toggle Plain Text)
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:
Python Syntax (Toggle Plain Text)
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: Perhaps others can come up with a better way, but you'd basically do something like:
I don't really have a good handle on Python's type() function, but you can use it as ff:
Python Syntax (Toggle Plain Text)
>>> str(type(56)) "<type 'int'>" >>> str(type('ABC')) "<type 'str'>" >>> str(type(False)) "<type 'bool'>" >>>
Python Syntax (Toggle Plain Text)
NothingTypes = {"<type 'int'>": 0, "<type 'str'>":"", "<type 'bool'>": False} # etc ... MyValue = NothingTypes[type(MyVariable)]
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
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.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
•
•
•
•
It looks like Nothing is used to declare a variable for use at a later time.
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:
VB Syntax (Toggle Plain Text)
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:
VB Syntax (Toggle Plain Text)
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!
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Pass in the appropriate "Nothing" value for that type.
So if the COM function header is
then you would pass in
But see also:
http://mail.python.org/pipermail/pyt...il/000902.html
http://osdir.com/ml/python.ctypes/2003-06/msg00055.html
Jeff
So if the COM function header is
VB Syntax (Toggle Plain Text)
Private Function MyFunc(text as String) as String
then you would pass in
VB Syntax (Toggle Plain Text)
MyFunc("") ' "" is the equivalent to Nothing
But see also:
http://mail.python.org/pipermail/pyt...il/000902.html
http://osdir.com/ml/python.ctypes/2003-06/msg00055.html
Jeff
•
•
Join Date: Feb 2009
Posts: 4
Reputation:
Solved Threads: 0
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?
File "<COMObject <unknown>>", line 2, in AccessSelections
pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 2)
What do you think I should do?
![]() |
Similar Threads
- How to do Input in Python? (Python)
- What are the .apy files in Python (Python)
- Python! Thoughts? (Python)
- what is python (Python)
- design customized mysql frontend with perl or python (MySQL)
Other Threads in the Python Forum
- Previous Thread: The tarfile module
- Next Thread: calculating standard deviation
Views: 1206 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for Python
advanced anydbm app bash beginner bits calling chmod cmd code data dictionary directory dynamic edit examples excel external feet file float format ftp function gui halp homework http i/o images import info input ip itunes java keycontrol line linux list lists loan loop maintain millimeter mouse newb number numbers output panel parsing path port prime print program programming projects push py-mailer py2exe pygame pyqt python queue random recursion recursive scrolledtext smtp split ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update urllib urllib2 variable variables ventrilo web webservice whileloop windows wxpython xlib






