Advertisement
dewabe

SimpleSample: Class

Aug 14th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #!/usr/bin/python
  2. # SimpleSample Class
  3.  
  4. class sensor(object):
  5.     def __init__(self, quantity, unit, value=None):
  6.         self.quantity = quantity
  7.         self.value = value
  8.         self.unit = unit
  9.  
  10.     def store_value(self, value):
  11.         self.value = value
  12.  
  13.     def __str__(self):
  14.         return "{quantity} has a value of {value} {unit}".format(
  15.             quantity = self.quantity,
  16.             value = self.value,
  17.             unit = self.unit)
  18.  
  19.  
  20. humidity = sensor("Humidity", "%", 65)
  21. print(humidity) # Output: Humidity has a value of 65 %
  22.  
  23. temperature = sensor("Temperature", "'C")
  24. temperature.store_value(12.2)
  25. print(temperature) # Temperature has a value of 12.2 'C
  26. print(temperature.quantity) # Temperature
  27. print(temperature.value) # 12.2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement