943,865 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2653
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 16th, 2009
0

'None' in Python and 'Nothing' in VB

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
artmansoft is offline Offline
4 posts
since Feb 2009
Feb 16th, 2009
0

Re: 'None' in Python and 'Nothing' in VB

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:

VB Syntax (Toggle Plain Text)
  1. Dim a as Integer
  2. a = Nothing

is equivalent to

VB Syntax (Toggle Plain Text)
  1. Dim a as Integer
  2. 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)
  1. NothingTypes = {"Integer": 0, "Boolean": False, ...}
  2.  
  3. ...
  4.  
  5. MyValue = NothingTypes[TypeToString(MyVar)]

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

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 16th, 2009
0

Re: 'None' in Python and 'Nothing' in VB

Good explanation, Jeff.

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)
  1. >>> str(type(56))
  2. "<type 'int'>"
  3. >>> str(type('ABC'))
  4. "<type 'str'>"
  5. >>> str(type(False))
  6. "<type 'bool'>"
  7. >>>
Perhaps others can come up with a better way, but you'd basically do something like:
Python Syntax (Toggle Plain Text)
  1. NothingTypes = {"<type 'int'>": 0, "<type 'str'>":"", "<type 'bool'>": False} # etc
  2. ...
  3. MyValue = NothingTypes[type(MyVariable)]
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007
Feb 17th, 2009
0

Re: 'None' in Python and 'Nothing' in VB

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
artmansoft is offline Offline
4 posts
since Feb 2009
Feb 17th, 2009
0

Re: 'None' in Python and 'Nothing' in VB

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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Feb 17th, 2009
0

Re: 'None' in Python and 'Nothing' in VB

Quote ...
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:

VB Syntax (Toggle Plain Text)
  1. Dim a as Integer 'declares a
  2.  
  3. 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)
  1. Dim a as Integer
  2. a = Nothing
  3.  
  4. GetPossibleNewValue(a) 'might change a, might not
  5. print a

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

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 17th, 2009
0

Re: 'None' in Python and 'Nothing' in VB

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!
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Feb 18th, 2009
0

Re: 'None' in Python and 'Nothing' in VB

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
artmansoft is offline Offline
4 posts
since Feb 2009
Feb 18th, 2009
0

Re: 'None' in Python and 'Nothing' in VB

Pass in the appropriate "Nothing" value for that type.

So if the COM function header is

VB Syntax (Toggle Plain Text)
  1. Private Function MyFunc(text as String) as String

then you would pass in

VB Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 20th, 2009
0

Re: 'None' in Python and 'Nothing' in VB

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
artmansoft is offline Offline
4 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: The tarfile module
Next Thread in Python Forum Timeline: calculating standard deviation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC