tarkoon
tarkoon

Reputation: 23

How do I test to see if a variable is part of a specific class?

Another beginner question that I hope you all could help me with.

I am trying to compare the stats of different cars together. To do so, I separated them all into different classes and now would like to take those classes and compare their numbers together. I think I have a general idea on how to do the actual comparison, but I am wondering how can I test the variable (Or even just have it print out the objects name) to see which variable belongs to which class?

Example the Prius gets 51 miles on the highway, and so it will be the clear winner if I'm trying to find the top mpg vehicle. I'd like to figure out a way to know that I am actually talking about the Prius when I find that number. Maybe like 'Prius: 51 mpg' or something to that manner.

Hopefully, that makes sense. If not, I will clarify to the best of my ability! I know that it's probably an easy answer, but for some reason, I can't figure this out...

Thank you all for your help!

class Impala:
    mpg_h = 27
    mpg_c = 17
    fuel_size = 17

class Prius:
   mpg_h = 51
   mpg_c = 48
   fuel_size = 11.9

class CivicHybrid:
   mpg_h = 40
   mpg_c = 45
   fuel_size = 12.3

def top_mpg(*arg):
   high = (max(arg))

top_mpg(Impala.mpg_h, Prius.mpg_h, CivicHybrid.mpg_h)

also a bonus question... How would I get my code to automatically input all of the different classes into the function for the fuel size for example? I plan on comparing up to 50 cars at a time, but writing them all in seems like a pain, and that there might be an easier way to do that...

Upvotes: 0

Views: 64

Answers (1)

Kevin He
Kevin He

Reputation: 1250

First of all - for the question in your title, I'm not sure if I understand 100% what you are asking. You can use isinstance to test if something belongs to a certain class, e.g. isinstance(3, int) is True. Or if you define a custom class A and a is an instance of A, isinstance(a, A) is True. If you want to test if an object have a certain attribute, you can test it with the hasattr built-in function. For example, strings in python has a method strip, then hasattr('abc', 'strip') is True.

It seems your cars have the same recipe - why don't group them together like this

class Car:
    def __init__(self, make, mpg_h, mpg_c, fuel_size):
        self.make = make
        self.mpg_h = mpg_h
        self.mpg_c = mpg_c
        self.fuel_size = fuel_size

    def __str__(self):    
        return '<Car of make {}>'.format(self.make)

Then you create the models like this:

prius = Car('Prius', 51, 48, 11.9)
civic_hybrid = Car('CivicHybrid', 40, 100, 12.3)
...

Then you can compare them with different metrics (you can pass in the function that gets the key for comparison to the key optional argument) and you can see their makes

max_mpg_h = max([prius, civic_hybrid], key=lambda x: x.mpg_h)
print(max_mpg_h) # prints <Car of make Prius>

max_mpg_c = max([prius, civic_hybrid], key=lambda x: x.mpg_c)
print(max_mpg_c) # prints <Car of make CivicHybrid>

Upvotes: 1

Related Questions