Тёмный

Python Class Constructors and Instance Initialization 

Real Python
Подписаться 194 тыс.
Просмотров 17 тыс.
50% 1

Опубликовано:

 

30 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 8   
@eles108
@eles108 2 года назад
Thanks
@LuigiBrotha
@LuigiBrotha 13 дней назад
Why is isinstance to store the class around 11:03 . Type would be much clearer here
@Squash101
@Squash101 2 месяца назад
is there some sort of playlist for this?
@pythonicman6074
@pythonicman6074 2 года назад
انت مبدع
@serychristianrenaud
@serychristianrenaud 2 года назад
Thank
@orangasli2943
@orangasli2943 2 года назад
I believe there is no initializer list-like where you just need to pass in the constant value to the constructor as parameters.. Let say I have a class "Point" which has x,y,z to be passed in the Point to constructor..it is fine.. But then if I make a class "Triangle" I want to take three "Points" with it When defining Triangle object I have difficulty passing in the parameters like in c++ using the initializer list with the '{' '}' and pass in the constant/literal accordingly..but there are no options like this in python.. I need to give the name first to all the points before I can pass it to the triangle constructor.. Is there any way I can do this? I tried using the *args by python but it seems it can only used for one tuple not more than that..I requested three tuples but it says it cannot do it.. I guess I want to write code back in c++
@rdean150
@rdean150 Год назад
You can indeed do this with *args. class Point: def__init__(self, x, y, z): self.x = x self.y = y self.z = z def __repr__(self): return f"Point({self.x}, {self.y}, {self.z})" class Triangle: def __init__(self, *points): if len(points) != 3: raise ValueError() self.points = points t = Triangle( *[Point(1, 2, 3), Point(4, 5, 6), Point(7, 8, 9)] ) for point in t.points: print(point)
@rdean150
@rdean150 Год назад
You can do the same with named attributes by using **kwargs instead of *args. But the important thing here is that this requires the __init__ to accept the arguments, which is not quite the same as initializer lists, which allow arbitrary assignment of attributes, regardless of constructor signature. There are succint ways if doing arbitrary names assignments in python also, but still not exactly like initializer lists.
Далее
Learn Python OOP in under 20 Minutes
18:32
Просмотров 86 тыс.
СОБАКА И  ТРИ ТАБАЛАПКИ Ч.2 #shorts
00:33
🤍ПОЮ для ВАС ВЖИВУЮ🖤
3:04:40
Просмотров 1,3 млн
На кого пойти учиться?
00:55
Просмотров 202 тыс.
If __name__ == "__main__" for Python Developers
8:47
Просмотров 410 тыс.
Python's property() and Managing Attributes
28:45
Просмотров 6 тыс.
8 things in Python you didn't realize are descriptors
14:21
This Is Why Python Data Classes Are Awesome
22:19
Просмотров 812 тыс.
What exactly is 'self' in Python? [Easy explanation]
6:27
PLEASE Use These 5 Python Decorators
20:12
Просмотров 118 тыс.