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