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 11 1734
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
Thank you so much it answers my humble question perfectly:)
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 ...
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 ...
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.
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.
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('@')])"
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('@')])"
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('@')])"
In article <11**********************@b28g2000cwb.googlegroups .com>,
<br*********@gmail.comwrote:
I've learned a lot from you two, thank you:)
Peace This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: strout |
last post by:
function F(e)
{
return
function(){P(e)}
}
Can anybody tell me what the code is doing?
If return another function all in a function I would do
function F(e)
|
by: Jose Fernandez |
last post by:
Hello.
I'm building a web service and I get this error.
NEWS.News.CoverNews(string)': not all code paths return a value
This is the WebMethod
public SqlDataReader CoverNews(string Sport)...
|
by: Daylor |
last post by:
how can i return string to .Net application from c dll ?
i have the text in
char sText ;
and i want to return it from c dll to .Net application.
i tried : *char in the function, but the...
|
by: krwill |
last post by:
I'm trying to automate a combo box to add a record to the source table
if it's "Not In List". I've tried many different examples and none
have worked.
Combo Box Name = Combo24
Source Table...
|
by: John Nagle |
last post by:
Here's a wierd problem:
I have a little test case for M2Crypto, which just opens up SSL connections to
web servers and reads their certificates. This works fine.
But if I execute
...
| |
by: arnuld |
last post by:
/* C++ Primer 4/e
* section 3.2 - String Standard Library
* exercise 3.10
* STATEMENT
* write a programme to strip the punctation from the string. */
#include <iostream>
#include...
|
by: jk2l |
last post by:
Error 10 error LNK2019: unresolved external symbol __imp__glBindTexture@8 referenced in function "public: void __thiscall GLTexture::Use(void)" (?Use@GLTexture@@QAEXXZ) GLTexture.obj
Error 11 error...
|
by: askalottaqs |
last post by:
i created a function which u send a string, and it tokenizes according to spaces, so i send it a string for it to return me a string array, but on the declaration of the function it says
error...
|
by: lostlander |
last post by:
In ARMCC, and Microsoft C, when i use a function which is never
defined or delared, it gives out a warning, not a compiling error?
why?
(This leads to a bug to my program since I seldom pay much...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |