Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Celsius:
- def __init__(self, temperature = 0):
- self.temperature = temperature
- def to_fahrenheit(self):
- return (self.temperature * 1.8) + 32
- def get_temperature(self):
- print("Getting value")
- return self._temperature
- def set_temperature(self, value):
- if value < -273:
- raise ValueError("Temperature below -273 is not possible")
- print("Setting value")
- self._temperature = value
- temperature = property(get_temperature,set_temperature)
- # --------------------------------------
- def working(aclass, method_name, set_value):
- amount = aclass()
- amount.temperature = set_value
- # get access to method using its name as string
- method = getattr(amount, method_name)
- # execute method
- print(method())
- # send method name as string
- working(Celsius, 'to_fahrenheit', 373)
- # --------------------------------------
- def working2(obj, method, set_value):
- obj.temperature = set_value
- print(method())
- c = Celsius()
- working2(c, c.to_fahrenheit, 373)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement