'None' in Python and 'Nothing' in VB

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 4
Reputation: artmansoft is an unknown quantity at this point 
Solved Threads: 0
artmansoft artmansoft is offline Offline
Newbie Poster

'None' in Python and 'Nothing' in VB

 
0
  #1
Feb 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

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

 
0
  #2
Feb 16th, 2009
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:

  1. Dim a as Integer
  2. a = Nothing

is equivalent to

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

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

 
0
  #3
Feb 16th, 2009
Good explanation, Jeff.

I don't really have a good handle on Python's type() function, but you can use it as ff:
  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:
  1. NothingTypes = {"<type 'int'>": 0, "<type 'str'>":"", "<type 'bool'>": False} # etc
  2. ...
  3. MyValue = NothingTypes[type(MyVariable)]
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 4
Reputation: artmansoft is an unknown quantity at this point 
Solved Threads: 0
artmansoft artmansoft is offline Offline
Newbie Poster

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

 
0
  #4
Feb 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

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

 
0
  #5
Feb 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

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

 
0
  #6
Feb 17th, 2009
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:

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,474
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

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

 
0
  #7
Feb 17th, 2009
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!
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 4
Reputation: artmansoft is an unknown quantity at this point 
Solved Threads: 0
artmansoft artmansoft is offline Offline
Newbie Poster

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

 
0
  #8
Feb 18th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

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

 
0
  #9
Feb 18th, 2009
Pass in the appropriate "Nothing" value for that type.

So if the COM function header is

  1. Private Function MyFunc(text as String) as String

then you would pass in

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 4
Reputation: artmansoft is an unknown quantity at this point 
Solved Threads: 0
artmansoft artmansoft is offline Offline
Newbie Poster

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

 
0
  #10
Feb 20th, 2009
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1206 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC