How to return an "not string' error in function?

B

breakfastea

first of all I have to claim that I'm a noob so please help me don't
blame me:)

for example:

def test(s):
if type(s) != ? :
return
#So here I want establish a situation about that if <s> is not string
#then <return>, but how should write the <?> ?
#Or is there any other way to do it?

Any suggestion would be appreciate

Peace
 
T

Tim Chase

def test(s):
if type(s) != ? :
return
#So here I want establish a situation about that if <s> is not string
#then <return>, but how should write the <?> ?
#Or is there any other way to do it?
True

This will return true for both regular strings and for unicode
strings. If that's a problem, you can use
True

....or, if you don't want to qualify them with "types." each time,
you can use

to bring them into the local namespace.

HTH,

-tkc
 
D

Duncan Booth

Tim Chase said:
This will return true for both regular strings and for unicode
strings. If that's a problem, you can use

True

...or, if you don't want to qualify them with "types." each time,
you can use


to bring them into the local namespace.

They already are in the builtin namespace under their more usual names of
str and unicode respectively, so there is no need to import them, just use
them:
True

.... etc ...
 
C

Cameron Laird

Thank you so much it answers my humble question perfectly:)

HOWEVER, to answer you final question, yes, there is a different
and, in general, better, way. While there's a lot to say about
good Python style and typing, I'll summarize at a high level:
you shouldn't have to check types. I can understand that you
are working to make a particular function particularly robust,
and are trying to account for a wide range of inputs. This is
healthy. In stylish Python, though, you generally don't need
type checking. How would it be, for example, if someone passed
the number 3 to your function. Is that an error? Do you want
it automatically interpreted as the string "3"? You can achieve
these results withOUT a sequence of

if isinstance(...
elif isinstance(...
...

perhaps with something as simple as

my_input = str(my_input).

One of us will probably follow-up with a reference to a more
detailed write-up of the subject.
 
B

breakfastea

Thank you for your inputing which has been great inspirational:)

What I tried to do is to write a string.split() module, so I started
with:

def spilt(a):
l=[]
index=0
if not isinstance(a, basestring): #Or isinstance(a, str)
return
for i in len(a):
if a=' ':
item=a[index:i]
l.append(item)
..................

I'm still working on it:)

Thank you again

Peace

PS: Is str() the same as repr() ?
 
B

breakfastea

Thank you for your reminder:)

However I saw the split() function in the first place and that why I'm
trying write one myself:)

Peace

Bruno said:
Thank you for your inputing which has been great inspirational:)

What I tried to do is to write a string.split() module,

So don't waste time:
"ab eced f aazaz".split() ['ab', 'eced', 'f', 'aazaz']
"ab-eced-ff-aazaz".split('-') ['ab', 'eced', 'ff', 'aazaz']
 
B

Bruno Desthuilliers

Bruno said:
What I tried to do is to write a string.split() module,
So don't waste time:
"ab eced f aazaz".split() ['ab', 'eced', 'f', 'aazaz']
"ab-eced-ff-aazaz".split('-')
['ab', 'eced', 'ff', 'aazaz']
Thank you for your reminder:)

However I saw the split() function in the first place and that why I'm
trying write one myself:)

Ok. Then if it's for learning, let's have a look :)
def spilt(a):

def split(astring, sep=' '):
l=[]
index=0
if not isinstance(a, basestring): #Or isinstance(a, str)
return

It's an error, so you don't want to pass it silently:

if not isinstance(astring, basetring):
raise TypeError('expected a string or unicode, got : %s' \
% type(astring)
for i in len(a):

The common idiom is : "for item in sequence:". If you need indexes too,
use enumerate: "for i, char in enumerate(astring):".

But anyway, you may want to have a look at str.find() or str.index().
if a=' ':
item=a[index:i]
l.append(item)



Good luck...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top