0

Basically, I need to check the type of data of which a variable is storing before replacing the data stored in the variable with the new data. For example, how to check if the variable is storing a string data or an integer data?

Source Code:

class Toy:

    #Toy Class Constructor
    def __init__(self):
        Name = "Train Engine";
        ID = "TE11";
        Price = 0.99;
        Minimum_Age = 4;

    #Return Name
    def Return_Name(self):
        print(Name)
        return Name

    #Set Name
    def Set_Name(self, Variable):
        #This is where I would need to check the type of data that the variable 'Variable' is currently storing.
        Name = Variable

    #Return ID
    def Return_ID(self):
        print(ID)
        return ID

    #Set ID
    def Set_ID(self, Variable):
        #This is where I would need to check the type of data that the variable 'Variable' is currently storing.
        ID = Variable

    #Return Price
    def Return_Price(self):
        print(Price)
        return Price

    #Set Price
    def Set_Price(self, Variable):
        #This is where I would need to check the type of data that the variable 'Variable' is currently storing.
        Price = Variable

    #Return Minimum_Age
    def print_Minimum_Age(self):
        print(Minimum_Age)
        return Minimum_Age

    #Set Minimum_Age
    def Set_Minimum_Age(self, Variable):
        #This is where I would need to check the type of data that the variable 'Variable' is currently storing.
        Minimum_Age = Variable

So basically, how should I, or are there any conventional way to check the type of data that the variable is storing?

Marc
  • 75
  • 1
  • 2
  • 10
  • 1
    You can use `type`. – jsmolka Feb 27 '18 at 14:25
  • `type(variable_name)` use it – Nandish Patel Feb 27 '18 at 14:26
  • Possible duplicate of [How to check if type of a variable is string?](https://stackoverflow.com/questions/4843173/how-to-check-if-type-of-a-variable-is-string) – Ignacio Vergara Kausel Feb 27 '18 at 14:27
  • 1
    The only times where isinstance() is necessary is when checking inheritance of a given class compared to another, as you well said and referenced. type() shall be only used to check whether an instance is exactly of a given base type. thanks to @zmo here's the link https://stackoverflow.com/questions/21894575/isinstancefoo-bar-vs-typefoo-is-bar – Nandish Patel Feb 27 '18 at 14:29
  • There is a subtle distinction here: variables don't have types, *values* do. – chepner Feb 27 '18 at 14:42
  • Why do you need to store either a string or an integer in `Name` (which, by the way, needs to be an instance attribute, not a local variable)? A better approach would be for `Set_Name` to *accept* either a string or integer, but only *store* a value of a single type, converting one to the other as necessary. – chepner Feb 27 '18 at 14:43
  • The point is somewhat moot; in Python, you typically don't bother with such trivial getter and setter methods; just assign to and read from the attributes directly. – chepner Feb 27 '18 at 14:46

4 Answers4

5

Proper way to do this is isinstance

if isinstance(variable, MyClass)

But think twice if you actually need this. Python uses duck-typing, so explicit checks for types is not always a good idea. If you still want to do so, consider using some abstract base or minimal valuable type for your checks.

As other people suggesting, just getting type of your variable can be done by type(variable), but in most cases its better to use isinstance, because this will make your code polymorphic - you'll automatically support instances of subclasses of target type.

Slam
  • 8,112
  • 1
  • 36
  • 44
  • So basically if I want to check the data type of a string, then I would use 'isinstance(Variable, str)' ? – Marc Feb 27 '18 at 14:46
  • In python3 - yes. In python2 - `isinstance(var, basesting)` in most cases — to operate with `str` as well as with `unicode` – Slam Feb 27 '18 at 14:48
  • Thanks, that did the deal – Marc Feb 27 '18 at 14:51
0

If you really want the type of a variable, and don't want to support inheriting, use the built-in type function:

if type(variable) is MyClass:
    ...

I agree with @Slam, that you should use this responsibly.

Graipher
  • 6,891
  • 27
  • 47
0

type(variable_name) returns the type of the variable

Nikki
  • 3
  • 4
0

type(variable)

This command will return the type of the data stored by the variable.