check whether a value is scalar

E

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
 
R

Roy Smith

"Eli said:
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:
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).
 
H

harold

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"?
 
E

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).
 
F

Fredrik Lundh

Eli said:
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>
 
B

Bruno Desthuilliers

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.
 

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
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top