123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- class BankAccount:
- def __init__(self, name, balance):
- self.name = name
- self.__balance = balance
- @property
- def my_balance(self):
- return self.__balance
- @my_balance.setter
- def my_balance(self, value):
- if not isinstance(value, (int, float)):
- raise ValueError('Баланс должен быть числом')
- @my_balance.deleter
- def my_balance(self):
- del self.__balance
- class MagicBox:
- def __init__(self, contents=None):
- self._contents = contents
- @property
- def contents(self):
- if self._contents == "rabbit":
- return "A magical rabbit!"
- else:
- return self._contents
-
- @contents.setter
- def entity(self, new_contents):
- if new_contents == "wishes":
- print("Your wishes are granted!")
- self._contents = new_contents
- else:
- print("the magic didn't work this time.")
- self._contents = new_contents
- class Celsius:
- def __init__(self, temp) -> None:
- self.__temperature = temp
- def to_fahrenheit(self):
- return self.__temperature*9/5 + 32
- @property
- def temperature(self):
- return self.__temperature
- @temperature.setter
- def temperature(self, value):
- if value < -273.15:
- raise ValueError("Температура должна быть больше 273.15")
- else:
- self.__temperature = value
- class File:
- def __init__(self, size_in_bytes):
- self._size_in_bytes = size_in_bytes
- @property
- def size(self):
- if self._size_in_bytes < 1024:
- return f"{self._size_in_bytes} B"
- elif self._size_in_bytes < 1048576:
- return "%.2f KB" % (self._size_in_bytes/1024.0)
- elif self._size_in_bytes < 1073741824:
- return "%.2f MB" % (self._size_in_bytes/1048576.0)
- else:
- return "%.2f GB" % (self._size_in_bytes/1073741824.0)
- @size.setter
- def size(self, value):
- self._size_in_bytes = value
- class Notebook:
- def __init__(self, notes: list):
- self._notes = notes
- @property
- def notes_list(self):
- for i, j in enumerate(self._notes):
- print(f"{i+1}.{j}")
- def main():
- # acc = BankAccount('Ivan', 200)
- # print(acc.my_balance)
- note = Notebook(['Buy Potato', 'Buy Carrot', 'Wash car'])
- note.notes_list
- '''
- box = MagicBox("rubies")
- print(box.contents)
- print(box._contents)
- box.entity = "wishes"
- print(box._contents)
- print(box.entity)
- '''
- '''
- cel = Celsius(12.0)
- cel.temperature = 15
- cel.temperature = -300
- print(cel.temperature)
- '''
- if __name__ == '__main__':
- main()
|