Python Forum
Type conversion issue while using Descriptor in Python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Type conversion issue while using Descriptor in Python3
#1
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?


#!/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)
Reply
#2
This should point out the problem. You are not seeing Celsius.__Fahrenheit
t1.celsius=10
print(t1.fahrenheit, t1.celsius)
Output:
32 10.0
class Temperature():
    def __init__(self, fahrenheit):
        self.__fahrenheit = fahrenheit

    @property
    def celsius(self):
        return (self.__fahrenheit - 32) * 5/9

    @celsius.setter
    def celsius(self, value):
        self.__fahrenheit = value * 9/5 + 32
 
    @property
    def fahrenheit(self):
        return self.__fahrenheit

    @fahrenheit.setter
    def fahrenheit(self, value):
        self.__fahrenheit = float(value)

t1 = Temperature(32)
print(t1.fahrenheit, t1.celsius)
t1.celsius=10
print(t1.fahrenheit, t1.celsius)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question about descriptor akbarza 5 617 Jan-09-2024, 07:09 AM
Last Post: Gribouillis
  Date Conversion issue Racer_x 2 1,788 Jan-10-2022, 02:40 PM
Last Post: Racer_x
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,956 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Data Type conversion error rajeevjagatap 2 4,343 Apr-15-2020, 03:29 PM
Last Post: rajeevjagatap
  Type hinting - return type based on parameter micseydel 2 2,491 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,915 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  httplib to http.client conversion issue zatlas1 1 2,580 Apr-12-2019, 05:21 PM
Last Post: Larz60+
  strange effect from duplicating a file descriptor Skaperen 1 2,056 Feb-18-2019, 08:20 PM
Last Post: Skaperen
  strange python3 issue tony1812 1 2,243 Sep-06-2018, 01:47 AM
Last Post: Larz60+
  Issue python3.6 while inheriting telnet library sourabhjaiswal92 4 4,101 May-09-2018, 05:20 AM
Last Post: sourabhjaiswal92

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020