T
Tuvas
I need a function that will tell if a given variable is a character or
a number. Is there a way to do this? Thanks!
a number. Is there a way to do this? Thanks!
I need a function that will tell if a given variable is a
character or a number. Is there a way to do this? Thanks!
.... print type(x)for x in ['3',3,3.1,3j]:
.... if type(x) is str: print "it's a string"for x in ['3',3,3.1,3j]:
In Python, all values have an associated type which you can query withTuvas said:I need a function that will tell if a given variable is a character or
a number. Is there a way to do this? Thanks!
I need a function that will tell if a given variable is a character or
a number. Is there a way to do this? Thanks!
You can test the type of the object as follows:Tuvas said:I need a function that will tell if a given variable is a character or
a number. Is there a way to do this? Thanks!
Tuvas said:I need a function that will tell if a given variable is a character or
a number. Is there a way to do this? Thanks!
What's your use case? This need is incommon in Python...
Steven said:Judging by the tone of the original poster's question, I'd say for sure he
is an utter Python newbie, probably a newbie in programming in general,
so I bet that what (s)he really wants is something like this:
somefunction("6")
-> It is a number.
somefunction("x")
-> It is a character.
So, at the risk of completely underestimating Tuvas' level of programming
sophistication, I'm going to answer the question I think he meant to ask:
how do I tell the difference between a digit and a non-digit?
import string
def isDigit(s):
if len(s) != 1:
# a digit must be a single character, anything more
# or less can't be a digit
return False
else:
return s in string.digits
If you know that you are only dealing with a single character c, never a
longer string, you can just use the test:
c in string.digits
Steven said:No offense to the four or five helpful folks who answered Tuvas with
explanations of type() and isinstance(), but there is such a thing as dumb
insolence. I should know, I've done it often enough: answer the question
the person asks, instead of the question you know he means.
Satisfying, isn't it? *wink*
Judging by the tone of the original poster's question, I'd say for sure he
is an utter Python newbie, probably a newbie in programming in general,
so I bet that what (s)he really wants is something like this:
somefunction("6")
-> It is a number.
somefunction("x")
-> It is a character.
So, at the risk of completely underestimating Tuvas' level of programming
sophistication, I'm going to answer the question I think he meant to ask:
how do I tell the difference between a digit and a non-digit?
import string
def isDigit(s):
if len(s) != 1:
# a digit must be a single character, anything more
# or less can't be a digit
return False
else:
return s in string.digits
If you know that you are only dealing with a single character c, never a
longer string, you can just use the test:
c in string.digits
I had the same thought, but reread the post. He asks "if a given
variable is a character or a number". I figured that even if he
is coming from another language he knows the difference between
"a given variable" and the "contents of a give variable". I guess
we will see.... ;-). This list is so good, he gets BOTH questions
answered.
Gary said:In Python, all values have an associated type which you can query with
type(v) thus:
from types import *
if type(v) == IntType:
... whatever ...
Several types would qualify as "numbers": IntType, FloatType, LongType,
and ComplexType,
and several as "strings": StringType and UnicodeType
On Mon, 12 Dec 2005 18:51:36 -0600, Larry Bates wrote:
[snippidy-doo-dah]
I had the same thought, but reread the post. He asks "if a given
variable is a character or a number". I figured that even if he is
coming from another language he knows the difference between "a given
variable" and the "contents of a give variable". I guess we will
see.... ;-). This list is so good, he gets BOTH questions answered.
The problem is, Python doesn't have variables (although it is
oh-so-tempting to use the word, I sometimes do myself). It has names in
namespaces, and objects.
It be a subtle difference, but an important one.
That's why, for instance, Python is neither call by reference nor call
by value, it is call by object.
In a very important sense, one which you should understand in order toTom said:On Mon, 12 Dec 2005 18:51:36 -0600, Larry Bates wrote:
[snippidy-doo-dah]
I had the same thought, but reread the post. He asks "if a given
variable is a character or a number". I figured that even if he is
coming from another language he knows the difference between "a given
variable" and the "contents of a give variable". I guess we will
see.... ;-). This list is so good, he gets BOTH questions answered.
The problem is, Python doesn't have variables (although it is
oh-so-tempting to use the word, I sometimes do myself). It has names in
namespaces, and objects.
In what sense are the names-bound-to-references-to-objects not variables?
You appear very confident of your ignorance ;-)No, it's just spin, bizarre spin for which i can see no reason. Python has
variables.
Couldn't!No, python is call by value, and it happens that all values are pointers.
Just like java, but without the primitive types, and like LISP, and like a
load of other languages. Python's parameter passing is NO DIFFERENT to
that in those languages, and those languages are ALL described as
call-by-value, so to claim that python does not use call-by-reference but
some random new 'call-by-object' convention is incorrect, unneccessary,
confusing and silly.
</rant>
I'm sure this has been argued over many times here, and we still
all have our different ideas, so please just ignore this post!
Steve said:In Python a name (*not* a "variable", though people do talk loosely
about "instance variables" and "class variables" just to be able to use
terms familiar to users of other to languages) is simply *bound* to a
value. The only storage that is required, therefore, is enough to hold a
pointer (to the value currently bound to the name).
Tom said:In what sense are the names-bound-to-references-to-objects not variables?
On Mon, 12 Dec 2005 18:51:36 -0600, Larry Bates wrote:
[snippidy-doo-dah]
I had the same thought, but reread the post. He asks "if a given
variable is a character or a number". I figured that even if he is
coming from another language he knows the difference between "a given
variable" and the "contents of a give variable". I guess we will
see.... ;-). This list is so good, he gets BOTH questions answered.
The problem is, Python doesn't have variables (although it is
oh-so-tempting to use the word, I sometimes do myself). It has names in
namespaces, and objects.
In what sense are the names-bound-to-references-to-objects not variables?
No, python is call by value, and it happens that all values are
pointers.
I'm sure this has been argued over many times here, and we still
all have our different ideas, so please just ignore this post!
Yes, it's a reference to an object of type string holding the valueSteven said:name = "spam spam spam spam"
the value of the variable "name" is a pointer, and not a string. Riiight.
def increment(n):
"""Add one to the argument changing it in place."""
# In Pascal, I would need the var keyword to get this behaviour,
# but Python is call by reference so all variables are passed
# by reference.
n += 1
x = 1
increment(x)
assert x == 2
but that doesn't work in Python either.
Xavier said:Now use a mutable type instead of an immutable int and you'll notice a
pass-by-reference behavior.
Yes, it's a reference to an object of type string holding the value
<spam spam spam spam>
That example is mightily flawed since Python's integers are immutable
objects.
Now use a mutable type instead of an immutable int and you'll notice a
pass-by-reference behavior.
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.