By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,274 Members | 2,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,274 IT Pros & Developers. It's quick & easy.

check whether a value is scalar

P: n/a
Eli
Hi,

I want to check whether a value is a scalar. A scalar can be:
- None (null)
- string
- number (integer, float)
- boolean
How can I validate a value is one of these types?

I care about the value only, and not its class methods.
An object is not a scalar, since it's not a simple value.
An array might be considered as scalar.
The issue is I want to keep a set of values to share among several
applications in different languages, and only scalar values can be
shared. Since objects are not the same in all languages, it's possible
to share only simple values.

-thanks

Apr 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <11**********************@i40g2000cwc.googlegroups .com>,
"Eli" <el*****@gmail.com> wrote:
Hi,

I want to check whether a value is a scalar. A scalar can be:
- None (null)
- string
- number (integer, float)
- boolean
How can I validate a value is one of these types?

I care about the value only, and not its class methods.
An object is not a scalar, since it's not a simple value.
An array might be considered as scalar.
The issue is I want to keep a set of values to share among several
applications in different languages, and only scalar values can be
shared. Since objects are not the same in all languages, it's possible
to share only simple values.

-thanks


I'm not sure what "scalar" means in this context. You say "an object is
not a scalar", but you also assert that an integer is a scalar. These are
contradictory statements, since integers *are* objects:
isinstance (1, object)

True

You say that numbers are scalars. All nunmbers, or just integers and
floats? What about complex numbers? What about long integers?

In any case, the way to check for a type is with isinstance(), as I did
above).
Apr 22 '06 #2

P: n/a
would

isinstance(value,(type(None),str,int,float,bool))

be enough? This yields true if the type value is in the list of type
objects given as second argument, or a subtype of one of them. What,
however, do you mean with "I care about the value only, and not its
class method"?

Apr 22 '06 #3

P: n/a
Eli
Python treats integers as objects, but as I mentioned that I do care
about the value only, and not its object methods. I mean that it's not
possible to share objects among application in different programming
languages, but it's possible to share the scalar values among them.
Strings, booleans, integeres, floats, null are types that most
programming languages use. Arrays are also commonly used, but each
programming language defines and uses it differently, so it's more
problematic to treat it as scalar (for example python uses dictionaries
while other langs uses regular arrays only).

Apr 23 '06 #4

P: n/a
"Eli" <el*****@gmail.com> wrote:
The issue is I want to keep a set of values to share among several
applications in different languages, and only scalar values can be
shared. Since objects are not the same in all languages, it's possible
to share only simple values.


I can assure you that people who've implemented various kinds of
RPC protocols and cross-language bindings would be rather surprised
to hear that you've discovered that it's impossible to do what they've
done.

I'm not entirely convinced that you know what you're talking about,
really. Maybe you should spend a little more time studying prior art ?

</F>

Apr 23 '06 #5

P: n/a
Eli a écrit :
Python treats integers as objects, but as I mentioned that I do care
about the value only, and not its object methods. I mean that it's not
possible to share objects among application in different programming
languages, but it's possible to share the scalar values among them.
Not so easily. Lower level languages have some strange rules about size
of an integer, signed/unsigned stuff, precision issues for floats,
etc... - and of course different representations for a 'string'.
Strings, booleans, integeres, floats, null are types that most
programming languages use. Arrays are also commonly used, but each
programming language defines and uses it differently,
Same thing for strings.
so it's more
problematic to treat it as scalar (for example python uses dictionaries
Python's dicts are hastables, not arrays.
while other langs uses regular arrays only).

Apr 24 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.