简体   繁体   English

对在类中调用方法感到困惑(PYTHON)

[英]Confused about calling a method within a class (PYTHON)

So i've got a class for dealing with temperatures, it's really basic, but for some reason the conversion of celsius to fahrenheit is failing to return a correct result in one area of the class when it works otherwise. 因此,我有一个用于处理温度的类,它确实很基本,但是由于某种原因,将摄氏度转换为华氏温度无法正常工作时,无法在该类的某个区域返回正确的结果。

I'm writing the add method, and in order to add two temperatures of different types, they have to be converted. 我正在写add方法,为了添加两个不同类型的温度,必须将它们转换。 I have a method for switching between celsius and fahrenheit, and it works when I just do that in a test program: 我有一种在摄氏和华氏度之间切换的方法,当我在测试程序中执行此操作时,它就可以工作:

Testing celsius method:
Converting to celsius...
Result: Temperature: -12.222222222222221 degrees C

Testing fahrenheit method:
Converting back to fahrenheit...
Result: Temperature: 10.0 degrees F

But, if I try to use this to add two different types (extra prints to see what it's doing): 但是,如果我尝试使用它来添加两种不同的类型(请额外打印以查看其作用):

def __add__( self, other ):

    if type( other ) != Temperature:
        other = Temperature( other )

    a_scale = self.__scale

    if a_scale == 'F':
        print(self)
        print(other)
        temp_1 = self.fahrenheit()
        temp_2 = other.fahrenheit()
        print(temp_1)
        print(temp_2)
        temp_3 = temp_1.__magnitude + temp_2.__magnitude
        return Temperature( temp_3, 'F' )

    elif a_scale == 'C':
        print(self)
        print(other)
        temp_1 = self.celsius()
        temp_2 = other.celsius()
        print(temp_1)
        print(temp_2)
        temp_3 = temp_1.__magnitude + temp_2.__magnitude
        return Temperature( temp_3, 'C')

It will print out that it converted 10 degrees C to 68 degrees F (???) 它将打印出将10摄氏度转换为68华氏度(???)

output: 输出:

Testing addition
Temp 2 = Temperature: 20 degrees C
Performing temp1 + temp2...
Temperature: 10 degrees F
Temperature: 20 degrees C
Temperature: 10 degrees F
Temperature: 68.0 degrees F
Temperature: 78.0 degrees F

This is the fahrenheit method: 这是华氏方法:

def fahrenheit( self ):

    if self.__scale == 'F':
        return Temperature( self.__magnitude, 'F')
    elif self.__scale == 'C':
        f = ((self.__magnitude * 9) / 5) + 32
        return Temperature( f, 'F' )

And it works perfectly fine as far as I can tell. 据我所知,它工作得很好。

Celsius, which is almost identical: 摄氏,几乎相同:

def celsius( self ):

    if self.__scale == 'C':
        return Temperature( self.__magnitude, 'C')
    elif self.__scale == 'F':
        c = ((self.__magnitude - 32) * 5) / 9
        return Temperature( c, 'C' )

I can't for the life of me figure out why this is happening :( 我一生无法弄清为什么会这样:(

You seem to be misreading the output of your own program. 您似乎误读了自己程序的输出。 The values outputted are: 输出的值是:

Temperature: 10 degrees F    # self
Temperature: 20 degrees C    # other
Temperature: 10 degrees F    # temp_1 = self.fahrenheit()
Temperature: 68.0 degrees F  # temp_2 = other.fahrenheit()
Temperature: 78.0 degrees F  # The sum of the above two

Thus, 10°F becomes 10°F, and 20°C becomes 68°F, which is correct. 因此,10°F变为10°F,而20°C变为68°F,这是正确的。

I guess, the reason is whole number arithmetic. 我想,原因是整数运算。 Try ((self.__magnitude - 32) * 5) / 9.0 and ((self.__magnitude * 9) / 5.0) + 32 . 尝试((self.__magnitude - 32) * 5) / 9.0((self.__magnitude * 9) / 5.0) + 32 I've performed a simple test (w/o class stuff, just math) and it gives correct results. 我执行了一个简单的测试(没有类的东西,只是数学),它给出了正确的结果。

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

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