Обновить less7.md
This commit is contained in:
parent
47dba56253
commit
bdb5af1292
24
less7.md
24
less7.md
|
@ -1,6 +1,7 @@
|
|||
[Ссылка на урок](https://youtu.be/iJu8UnKAWfA?si=wCmaFZE5mC9lkV--)
|
||||
|
||||
ООП в Python
|
||||
### ООП в Python ###
|
||||
```python
|
||||
class Product:
|
||||
count = 0
|
||||
|
||||
|
@ -35,16 +36,23 @@ class Product:
|
|||
|
||||
def __call__(self, *args, **kwargs):
|
||||
print(args, kwargs)
|
||||
Создание объекта
|
||||
|
||||
```
|
||||
|
||||
### Создание объекта ###
|
||||
```python
|
||||
table = Product()
|
||||
table.name = "Стол"
|
||||
table.price = 1500
|
||||
table.help()
|
||||
Атрибуты класса в Python
|
||||
```
|
||||
|
||||
### Атрибуты класса в Python ###
|
||||
```python
|
||||
# Атрибуты можно добавлять динамически
|
||||
table.color = "красный"
|
||||
print(dir(table))
|
||||
|
||||
# И удалять тоже
|
||||
del table.color
|
||||
print(dir(table))
|
||||
|
@ -52,16 +60,23 @@ print(dir(table))
|
|||
# Можно проверить сущестовование атрибута
|
||||
print("Есть атрибут color", hasattr(table, "color"))
|
||||
print("Есть атрибут name", hasattr(table, "name"))
|
||||
|
||||
# установить атрибут
|
||||
setattr(table, "material", "дуб")
|
||||
|
||||
# удалить
|
||||
delattr(table, "material")
|
||||
|
||||
# и получить
|
||||
print(getattr(table, "name"))
|
||||
Магические методы класса в Python
|
||||
```
|
||||
|
||||
### Магические методы класса в Python ###
|
||||
```python
|
||||
# Передача параметров при инициализации __init__(self, price=0)
|
||||
table_2 = Product(3500)
|
||||
print(table_2.price)
|
||||
|
||||
# Изменение представления объекта при распечатке __str__
|
||||
print(table_2)
|
||||
|
||||
|
@ -85,5 +100,6 @@ print(table_3)
|
|||
table_3(5, a=4)
|
||||
|
||||
print(Product.count)
|
||||
```
|
||||
|
||||
[Вернуться на главную](readme.md)
|
Loading…
Reference in New Issue