Created
February 17, 2016 19:56
-
-
Save ehermes/cb1a410c3b74160a9c32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Temperature(object): | |
| def __init__(self, T=0, unit='C'): | |
| if unit == 'C': | |
| self.C = T | |
| elif unit == 'F': | |
| self.F = T | |
| elif unit == 'K': | |
| self.K = T | |
| else: | |
| raise ValueError | |
| @property | |
| def C(self): | |
| return self.K - 273.15 | |
| @C.setter | |
| def C(self, T): | |
| self.K = T + 273.15 | |
| @property | |
| def F(self): | |
| return (self.C * 1.8) + 32 | |
| @F.setter | |
| def F(self, T): | |
| self.C = (T - 32) / 1.8 | |
| freezing = Temperature(0, 'C') | |
| print(freezing.C) | |
| print(freezing.F) | |
| print(freezing.K) | |
| boiling = copy.copy(freezing) | |
| boiling.C = 100 | |
| print(boiling.C) | |
| print(boiling.F) | |
| print(boiling.K) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment