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?

6
  • 1
    You can use type. Commented Feb 27, 2018 at 14:25
  • type(variable_name) use it Commented Feb 27, 2018 at 14:26
  • 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 stackoverflow.com/questions/21894575/… Commented Feb 27, 2018 at 14:29
  • There is a subtle distinction here: variables don't have types, values do. Commented Feb 27, 2018 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. Commented Feb 27, 2018 at 14:43

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

So basically if I want to check the data type of a string, then I would use 'isinstance(Variable, str)' ?
In python3 - yes. In python2 - isinstance(var, basesting) in most cases — to operate with str as well as with unicode
Thanks, that did the deal
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.

Comments

0

type(variable_name) returns the type of the variable

Comments

0

type(variable)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.