469,271 Members | 1,690 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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

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 <sis not string
#then <return>, but how should write the <??
#Or is there any other way to do it?

Any suggestion would be appreciate

Peace

Sep 21 '06 #1
11 1621
def test(s):
if type(s) != ? :
return
#So here I want establish a situation about that if <sis not string
#then <return>, but how should write the <??
#Or is there any other way to do it?
>>isinstance("hello", basestring)
True
>>isinstance(u"hello", basestring)
True

This will return true for both regular strings and for unicode
strings. If that's a problem, you can use
>>import types
isinstance("hello", types.StringType)
True
>>isinstance(u"hello", types.StringType)
False
>>isinstance("hello", types.UnicodeType)
False
>>isinstance(u"hello", types.UnicodeType)
True

....or, if you don't want to qualify them with "types." each time,
you can use
>>from types import StringType, UnicodeType
to bring them into the local namespace.

HTH,

-tkc


Sep 21 '06 #2
Thank you so much it answers my humble question perfectly:)

Sep 21 '06 #3
Tim Chase <py*********@tim.thechases.comwrote:
This will return true for both regular strings and for unicode
strings. If that's a problem, you can use
>import types
isinstance("hello", types.StringType)
True
>isinstance(u"hello", types.StringType)
False
>isinstance("hello", types.UnicodeType)
False
>isinstance(u"hello", types.UnicodeType)
True

...or, if you don't want to qualify them with "types." each time,
you can use
>from types import StringType, UnicodeType

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:
>>isinstance("hello", str)
True

.... etc ...
Sep 21 '06 #4
Or yes that seems a handy way:)

Thanks for all wonderful people here:)

Peace
Duncan Booth wrote:
Tim Chase <py*********@tim.thechases.comwrote:
This will return true for both regular strings and for unicode
strings. If that's a problem, you can use
>>import types
>>isinstance("hello", types.StringType)
True
>>isinstance(u"hello", types.StringType)
False
>>isinstance("hello", types.UnicodeType)
False
>>isinstance(u"hello", types.UnicodeType)
True

...or, if you don't want to qualify them with "types." each time,
you can use
>>from types import StringType, UnicodeType
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:
>isinstance("hello", str)
True

... etc ...
Sep 21 '06 #5
In article <11*********************@k70g2000cwa.googlegroups. com>,
<br*********@gmail.comwrote:
>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.
Sep 21 '06 #6
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[i]=' ':
item=a[index:i]
l.append(item)
..................

I'm still working on it:)

Thank you again

Peace

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

Cameron Laird wrote:
In article <11*********************@k70g2000cwa.googlegroups. com>,
<br*********@gmail.comwrote:
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.
Sep 21 '06 #7
br*********@gmail.com wrote:
(OT : please dont top-post)
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']
>>>


--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 21 '06 #8
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 Desthuilliers wrote:
br*********@gmail.com wrote:
(OT : please dont top-post)
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']
>>

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 21 '06 #9
br*********@gmail.com wrote:
(please, *stop* top-posting - corrected)
Bruno Desthuilliers wrote:
>>
br*********@gmail.com wrote:
(OT : please dont top-post)
>>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[i]=' ':
item=a[index:i]
l.append(item)

Good luck...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 21 '06 #10
In article <11**********************@b28g2000cwb.googlegroups .com>,
<br*********@gmail.comwrote:
Sep 21 '06 #11
I've learned a lot from you two, thank you:)

Peace

Sep 22 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by strout | last post: by
13 posts views Thread by arnuld | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.