May-07-2020, 10:03 AM
This is a little tricky one. Needs more attention since it is a minute thing. There are two classes in the given code. 'celsius' attribute of temperature is handled by 'Celsius' descriptor. Requirement is to have temperature maintained in both fahrenheit and celsius. When executed the code below, as per my understanding, the output should be
32 0.0
32.0 0.0
but the one we get is
32 0.0
32 0.0
Can anyone help me understand why the fahrenheit value in the second line is still an 'int' eventhough it is converted into 'float' in the __set__ method in the descriptor class?
32 0.0
32.0 0.0
but the one we get is
32 0.0
32 0.0
Can anyone help me understand why the fahrenheit value in the second line is still an 'int' eventhough it is converted into 'float' in the __set__ method in the descriptor class?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/bin/python3 import sys import os class Celsius(): def __get__( self , instance, owner): return float ( self .__celsius) def __set__( self , instance, value): self .__celsius = value self .__fahrenheit = float (value * 9 / 5 + 32 ) class Temperature(): celsius = Celsius() def __init__( self ,fahrenheit): self .fahrenheit = fahrenheit self .celsius = (fahrenheit - 32 ) * ( 5 / 9 ) t1 = Temperature( 32 )[size = medium][ / size] print (t1.fahrenheit, t1.celsius) t1.celsius = 0 print (t1.fahrenheit, t1.celsius) |