I don't know how to check if a variable is primitive. In Java it's like this:
if var.isPrimitive():
The java. lang. Class. isPrimitive() method can determine if the specified object represents a primitive type.
Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java. Such types serve only one purpose — containing pure, simple values of a kind.
The 8 Primitive Variable Typesbyte , short , int , long , float , double , char , boolean .
Since there are no primitive types in Python, you yourself must define what you consider primitive:
primitive = (int, str, bool, ...) def is_primitive(thing): return isinstance(thing, primitive)
But then, do you consider this primitive, too:
class MyStr(str): ...
?
If not, you could do this:
def is_primitive(thing): return type(thing) in primitive
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With