Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Ok, here are some easy points. PyBinding came with this script:

def IsNotNull(value):
    return value is not None

It is close, but what I want is this.

bool IsNotNullOrEmpty(string value) {
    return (value != null) && (value.Length > 0 );
}
share|improve this question
Well, .NET includes String.IsNullOrEmpty. Would that work? – ojrac Jan 25 '10 at 2:22

5 Answers

up vote 8 down vote accepted

To check if a string is empty you would use len. Try this:

def IsNotNull(value):
    return value is not None and len(value) > 0
share|improve this answer
Wrong again. Did you even bother trying this code? – Jonathan Allen Jan 25 '10 at 1:58
There you go, I've rolledback to my original answer. I deleted my post and then replaced it with what I believed to be a "Pythonic" solution, which involves the not not. – Brian McKenna Jan 25 '10 at 2:08
-1 This is not necessary in Python as None and '' are False in Python: docs.python.org/library/stdtypes.html#truth-value-testing – Lukas Cenovsky Jan 31 '10 at 18:41
@Lukas, view the history of this answer. Jonathon didn't like that solution and it doesn't always work for IronPython classes. – Brian McKenna Jan 31 '10 at 20:33
@Brian. Well, this is not Pythonic but it is fine hack to make PyBindig work if my last comment at @Ignacio's answer is true. – Lukas Cenovsky Feb 1 '10 at 14:37

You should not be doing this in a function. Instead you should just use:

if someStringOrNone:
share|improve this answer
Didn't work. Empty strings still came back as True. – Jonathan Allen Jan 25 '10 at 1:54
5  
This is the preferred Pythonic version and it does work perfectly for Python's strings. The only reason it might not work for you is if you are passing some .NET type that is not compatible with Python strings to the function. – Max Shawabkeh Jan 25 '10 at 2:13
@Jonathan I don't think you are passing empty string to the test. As Ignacio showed you, it works for empty strings. Can you show us code where empty string evalutes as True? I think that would be a bug then. – Lukas Cenovsky Jan 31 '10 at 18:28
The empty string was being stored in a WPF control and routed to an IronPython function via PyBinding. I have no idea what a "Python string" is or whether or not it is compatible with a System.String. – Jonathan Allen Feb 1 '10 at 4:43
I don't know PyBinding but this looks to me like PyBinding handles values as richer objects. So then its empty string is not '' but some instance of "PyBindingValue" class which is definitely not False. – Lukas Cenovsky Feb 1 '10 at 14:34
show 1 more comment

If it's IronPython, then why not use the default implementation of IsNullOrEmpty from System.String?

import clr
clr.AddReference('System')
import System
System.String.IsNullOrEmpty('') # returns True
System.String.IsNullOrEmpty(None) # returns True
System.String.IsNullOrEmpty('something') # returns False
share|improve this answer
def IsNotNullString(s):
    return bool(s)

Rules of Python boolean conversion.

share|improve this answer
Didn't work. Empty strings still came back as True. – Jonathan Allen Jan 25 '10 at 1:52
@JonathanAllen I just ran this in the python interpreter and it worked for me. In which version of python did you get a True? I'm using 2.7.3 – Saurav May 16 '12 at 0:41

i think,

if IsNotNull(value) {

is equivalent to

if not value:

for strings. so i think the function is not necessary in python.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.