简体   繁体   English

如何测试变量是否属于特定类?

[英]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. 例如,普锐斯在高速公路上行驶了51英里,因此如果我想寻找顶级mpg车辆,它将是无疑的赢家。 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. 可能像“ Prius:51 mpg”之类的东西。

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... 我计划一次比较多达50辆汽车,但是将它们全部写入似乎很痛苦,而且可能会有更简单的方法来做...

First of all - for the question in your title, I'm not sure if I understand 100% what you are asking. 首先-对于您标题中的问题,我不确定我是否100%理解您的要求。 You can use isinstance to test if something belongs to a certain class, eg isinstance(3, int) is True . 您可以使用isinstance测试某些东西是否属于某个类,例如isinstance(3, int)True Or if you define a custom class A and a is an instance of A , isinstance(a, A) is True . 或者,如果您自定义一个类Aa为实例Aisinstance(a, A)True If you want to test if an object have a certain attribute, you can test it with the hasattr built-in function. 如果要测试对象是否具有特定属性,可以使用hasattr内置函数进行测试。 For example, strings in python has a method strip , then hasattr('abc', 'strip') is True . 例如,python中的字符串具有方法strip ,则hasattr('abc', 'strip')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 然后,您可以将它们与不同的指标进行比较(可以传入获取用于与key可选参数进行比较的key函数),然后可以查看它们的用法

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM